Traverse through a HashSet in Java

14 Dec, 2021
As we all know HashSet elements are unordered so the traversed elements can be printed in any order. In order to perform operations over our HashSet such as insertion, deletion, updating elements than first we need to reach out in order to access the HashSet. below are few ways with which we can iterate over elements to perform any kind of operations o Set elements as listed below.
Methods:
Using for-each loopUsing forEach methodUsing Iterators
Using for-each loop
Using forEach method
Using Iterators
Method 1: Using for-each loop
It is another array traversing technique like for loop, while loop, do-while loop introduced in Java 5. It starts with the keyword for like a normal for-loop. Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name array traversing technique like for loop, while loop, do-while loop introduced in Java 5. 
Example:
Java
// Java program to demonstrate iteration over// HashSet using an Enhanced for-loop  import java.util.*;  class IterationDemo {    public static void main(String[] args)    {        // your code goes here        HashSet<String> h = new HashSet<String>();          // Adding elements into HashSet using add()        h.add("Geeks");        h.add("for");        h.add("Geeks");          // Iterating over hash set items        for (String i : h)             System.out.println(i);            }}
Geeks
for
Method 2: Using forEach() method of Stream class
Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation that is, it may traverse the stream to produce a result or a side-effect. 
Tip:  In Java 8 or above, we can iterate a List or Collection using forEach() method.
Example:
Java
// Java program to demonstrate iteration over// HashSet using forEach() method  import java.util.*;  class IterationDemo {    public static void main(String[] args)    {        // your code goes here        HashSet<String> h = new HashSet<String>();          // Adding elements into HashSet using add()        h.add("Geeks");        h.add("for");        h.add("Geeks");          // Iterating over hash set items        h.forEach(i -> System.out.println(i));    }}
Geeks
for
 
Method 3: Using an Iterator
The iterator() method is used to get an iterator over the elements in this set. The elements are returned in no particular order. Below is the java program to demonstrate it.
Example 
Java
// Java program to Illustrate Traversal over HashSet// Using an iterator  // Importing required classesimport java.util.*;  // Main classclass GFG {      // Main driver method    public static void main(String[] args)    {        // Creating empty HashSet by declaring object        // of HashSet class of string type        HashSet<String> h = new HashSet<String>();          // Adding elements into HashSet        // using add() method        h.add("Geeks");        h.add("for");        h.add("Geeks");          // Iterating over HashSet elements        // using iterator        Iterator<String> i = h.iterator();          // Holds true till there is single element remaining        // in the Set        while (i.hasNext())              // Printing the elements            System.out.println(i.next());    }}
Geeks
for
solankimayank
prachisoda1234
Java-Collections
java-hashset
Java-Set-Programs
Picked
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.