how to iterate over hashset in java code example
Example 1: how to iterate hashset in java 8
Set<String> set = new HashSet<String>();
for (String s : set) {
System.out.println(s);
}
set.forEach(System.out::println);
Example 2: HashSet iterator() method in java
import java.util.HashSet;
import java.util.Iterator;
public class HashSetIteratorMethodExample
{
public static void main(String[] args)
{
HashSet<String> hs = new HashSet<String>();
hs.add("Welcome");
hs.add("hello");
hs.add("world");
hs.add("core");
hs.add("java");
System.out.println("HashSet values are: " + hs);
Iterator<String> value = hs.iterator();
System.out.println("iterator values are: ");
while(value.hasNext())
{
System.out.println(value.next());
}
}
}