import java.util.Collections;Schauen wir uns an, was passiert, wenn Sie dieses Programm ausführen:
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
public class parcours_hashset {
public static void main(String[] args) {
HashSet< String> hset = neues HashSet< String> ();
hset.add("he1");
hset.add("he2");
hset.add("he3");
/*Advanced For Loop*/
System.out.println("Advanced For Loop");
for(String s: hset)
System.out.println(s);
/*while+Iterator-Schleife*/
System.out.println("while+Iterator-Schleife");
Iteratorit = hset.iterator();
while(it.hasNext())
System.out.println(it.next());
/*Aufzählung*/
System.out.println("while+Aufzählungsschleife");
// Abrufen des EnumerationEnumeration enumeration = Collections.enumeration(hset);
// durchlesen HashSet
while(enumeration.hasMoreElements())
System.out.println(enumeration.nextElement());
}
}
Loop for advancedDer iterator() wird verwendet, um einen Iterator durch die Elemente des HashSet oder Sets zu erhalten. Elemente werden gemischt und ohne bestimmte Reihenfolge zurückgegeben.
he3
he1
he2
While+Iterator
he3
he1< br />he2
While+Enumeration Loop
he3
he1
he2
Please disable your ad blocker and refresh the window to use this website.