1Set<String> set = new HashSet<String>();
2for (String s : set) {
3 System.out.println(s);
4}
5
6//Java 8:
7set.forEach(System.out::println);
1import java.util.HashSet;
2import java.util.Iterator;
3public class HashSetIteratorMethodExample
4{
5 public static void main(String[] args)
6 {
7 HashSet<String> hs = new HashSet<String>();
8 hs.add("Welcome");
9 hs.add("hello");
10 hs.add("world");
11 hs.add("core");
12 hs.add("java");
13 System.out.println("HashSet values are: " + hs);
14 // create an Iterator
15 Iterator<String> value = hs.iterator();
16 System.out.println("iterator values are: ");
17 while(value.hasNext())
18 {
19 System.out.println(value.next());
20 }
21 }
22}