Back
How can I iterate over a Set/HashSet without the following?
Iterator iter = set.iterator();while (iter.hasNext()) { System.out.println(iter.next());}
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
You can use an enhanced for loop:
Set<String> set = new HashSet<String>();
//populate setfor (String s : set) { System.out.println(s);}
//populate set
for (String s : set) {
System.out.println(s);
Or with Java8
set.forEach(System.out::println);
Want to learn Java? Check out the Java certification from Intellipaat.
31k questions
32.8k answers
501 comments
693 users