Open In App

Traverse through a HashSet in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  1. Using for-each loop
  2. Using forEach method
  3. 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);       
    }
}


Output

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));
    }
}


Output: 

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 classes
import java.util.*;
 
// Main class
class 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());
    }
}


Output

Geeks
for

Time complexity: O(n) to add n elements

Auxiliary space: O(n)



Last Updated : 15 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads