import java.util.Collections;Let's see what happens by running this program:
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 = new 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 Loop*/
System.out.println("While+Iterator Loop");
Iteratorit = hset.iterator();
while(it.hasNext())
System.out.println(it.next());
/*Enumeration*/
System.out.println("While+Enumeration Loop");
// retrieve the Enumeration
Enumerationenumeration = Collections.enumeration(hset);
// read through HashSet
while(enumeration.hasMoreElements())
System.out.println(enumeration.nextElement());
}
}
Loop for advancedThe iterator() is used to get an iterator through the elements of the HashSet or Set. Elements are returned mixed and without any specific order.
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.