Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

How can I iterate over a Set/HashSet without the following?

Iterator iter = set.iterator();

while (iter.hasNext()) {

    System.out.println(iter.next());

}

1 Answer

0 votes
by (13.1k points)

You can use an enhanced for loop:

Set<String> set = new HashSet<String>();

//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.

Related questions

0 votes
1 answer
0 votes
2 answers
asked Aug 15, 2019 in Java by Krishna (2.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...