How to Iterate HashSet in Java?
Last Updated :
17 Sep, 2024
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.
Methods to Iterate HashSet
There are three simple ways to iterate over a HashSet, which is the following :
- Using Iterator
- Without using Iterator (using for loop)
- 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 HashSet, so avoid by
// HashSet
set.add(10);
set.add(50);
// Duplicates not allowed in HashSet, so avoid by
// HashSet
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() + " ");
}
}
}
OutputIterate 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 HashSet, so avoid by
// HashSet
set.add("geeks");
set.add("for");
// duplicates not allowed in HashSet, so avoid by
// HashSet
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 + " ");
}
}
}
OutputIterate 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 HashSet, so avoid by
// HashSet
set.add("geeks");
set.add("for");
// duplicates not allowed in HashSet, so avoid by
// HashSet
set.add("geeks");
System.out.println(
"Iterate HashSet using forEach loop : ");
// Iterate throw a forEach method in Java
set.forEach(System.out::println);
}
}
OutputIterate HashSet using forEach loop :
Hello
geeks
for
on