Open In App

How to iterate HashSet in Java?

Last Updated : 15 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. It stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.

There are three simple ways to iterate over a HashSet, which is the following :

  1. Using Iterator
  2. Without using Iterator (using for loop)
  3. Using for-each loop

Method 1: Iterator method

In this method, we iterate HashSet with the help of iterator. First, we make an iterator to iterate HashSet with the help of the iterator() method in Java.

// Create a iterator of integer type to iterate HashSet

Iterator<Integer> it = set.iterator();

And then iterate HashSet with the help of hasNext() and Next() method in Java. Where hasNext() method check whether HashSet has elements or not and, Next() method access the data of HashSet.

Note: Use Next() method only one time after hasNext().

Example:

Java




// Java program to iterate the HashSet
// using iterator
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating a new HashSet for iteration
        HashSet<Integer> set = new HashSet<>();
  
        // Add data to HashMap
        set.add(10);
        set.add(20);
  
        // Duplicates not allowed in HashMap, so avoid by
        // HashMap
        set.add(10);
        set.add(50);
  
        // Duplicates not allowed in HashMap, so avoid by
        // HashMap
        set.add(50);
  
        // Create a iterator of type integer to iterate
        // HashSet
        Iterator<Integer> it = set.iterator();
  
        System.out.println(
            "Iterate HashSet using iterator : ");
  
        // Iterate HashSet with the help of hasNext() and
        // next() method
        while (it.hasNext()) {
  
            // Print HashSet values
            System.out.print(it.next() + " ");
        }
    }
}


Output

Iterate HashSet using iterator : 
50 20 10

Method 2: Iterate using for loop

In this method, we iterate HashSet using a simple for loop. Below is the implementation of this approach:

Example:

Java




// Java program to iterate the HashSet
// using for loop
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating a new HashSet for iteration
        HashSet<String> set = new HashSet<>();
  
        // Add data to HashSet
        set.add("Hello");
        set.add("geeks");
        set.add("on");
  
        //  duplicates not allowed in HashMap, so avoid by
        //  HashMap
        set.add("geeks");
        set.add("for");
  
        //  duplicates not allowed in HashMap, so avoid by
        //  HashMap
        set.add("geeks");
  
        System.out.println(
            "Iterate HashSet using for loop : ");
  
        // Iterate throw a simple for loop
        for (String ele : set) {
            // Print HashSet data
            System.out.print(ele + " ");
        }
    }
}


Output

Iterate HashSet using for loop : 
Hello geeks for on

Method 3: Using forEach() method

In this method, we iterate HashSet using forEach() loop in Java. Below is the implementation of this method:

Example:

Java




// Java program to iterate using forEach() loop
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating a new HashSet for iteration
        HashSet<String> set = new HashSet<>();
  
        // Add data to HashSet
        set.add("Hello");
        set.add("geeks");
        set.add("on");
  
        //  duplicates not allowed in HashMap, so avoid by
        //  HashMap
        set.add("geeks");
        set.add("for");
  
        //  duplicates not allowed in HashMap, so avoid by
        //  HashMap
        set.add("geeks");
  
        System.out.println(
            "Iterate HashSet using forEach loop : ");
  
        // Iterate throw a forEach method in Java
        set.forEach(System.out::println);
    }
}


Output

Iterate HashSet using forEach loop : 
Hello
geeks
for
on


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads