List the Elements of a Collection(iterate over the elements of set or list) in Java
Description
The following code shows how to list the Elements of a Collection(iterate over the elements of set or list).
Example
/* w w w . ja v a 2 s . co m*/
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] argv) throws Exception {
Set hSet = new HashSet();
hSet.add(new Integer("1"));
hSet.add(new Integer("2"));
hSet.add(new Integer("3"));
// For a set or list
for (Iterator it = hSet.iterator(); it.hasNext();) {
Object element = it.next();
System.out.println(element);
}
}
}
The code above generates the following result.