Java HashSet iterator() Method
The HashSet iterator() method in Java is used to return an iterator that can be used to iterate over the elements in a HashSet. HashSet does not maintain any specific order of its elements, so the elements are returned in random order when iterated over. The iterator() method provides a way to traverse the elements in the set, allowing you to access them one by one.
Example 1: Iterating over a HashSet of Integers
// Java Program to demonstrates the working of
// iterator() over a HashSet of Integers
import java.util.HashSet;
import java.util.Iterator;
public class Geeks {
public static void main(String[] args)
{
// Create a HashSet
HashSet<Integer> hs = new HashSet<>();
// Add elements to the HashSet
hs.add(10);
hs.add(20);
hs.add(30);
// Get the iterator
Iterator<Integer> i = hs.iterator();
// Traverse the HashSet using the iterator
System.out.println("HashSet: " + hs);
System.out.print("The iterator values are: ");
while (i.hasNext()) {
System.out.print(i.next() + " ");
}
}
}
// Java Program to demonstrates the working of
// iterator() over a HashSet of Integers
import java.util.HashSet;
import java.util.Iterator;
public class Geeks {
public static void main(String[] args)
{
// Create a HashSet
HashSet<Integer> hs = new HashSet<>();
// Add elements to the HashSet
hs.add(10);
hs.add(20);
hs.add(30);
// Get the iterator
Iterator<Integer> i = hs.iterator();
// Traverse the HashSet using the iterator
System.out.println("HashSet: " + hs);
System.out.print("The iterator values are: ");
while (i.hasNext()) {
System.out.print(i.next() + " ");
}
}
}
Output
HashSet: [20, 10, 30] The iterator values are: 20 10 30
Note: The order of elements in a HashSet is not guaranteed and can vary each time the program runs, as shown in the output.
Syntax of HashSet iterator() Method
public Iterator<E> iterator();
Return Type: This method returns an Iterator object to traverse the elements in the collection one by one.
Example 2: Iterating over a HashSet of Custom Objects
In this example, we will demonstrate how to store custom object "Employee" in a HashSet and iterate through them using an iterator.
// Java program to demonstrates the use of HashSet to store
// of HashSet to store custom objects and the traversal
// of these objects using an iterator
import java.util.HashSet;
import java.util.Iterator;
public class Geeks {
public static void main(String args[])
{
// create hash set
HashSet<Employee> hs = new HashSet<>();
hs.add(new Employee(10, "Geek1"));
hs.add(new Employee(2, "Geek20"));
hs.add(new Employee(35, "Geek5"));
// create an iterator
Iterator<Employee> i = hs.iterator();
// iterate through the HashSet and
// print each employee object
while (i.hasNext()) {
System.out.println("Value: " + i.next() + " ");
}
}
}
// create a class representing Employee
class Employee {
int id;
String name;
Employee(int id, String name)
{
this.id = id;
this.name = name;
}
// Override the toString() to provide the string
// representation of the Employee object
@Override public String toString()
{
return "[ " + this.id + ", " + this.name
+ " ]";
}
}
// Java program to demonstrates the use of HashSet to store
// of HashSet to store custom objects and the traversal
// of these objects using an iterator
import java.util.HashSet;
import java.util.Iterator;
public class Geeks {
public static void main(String args[])
{
// create hash set
HashSet<Employee> hs = new HashSet<>();
hs.add(new Employee(10, "Geek1"));
hs.add(new Employee(2, "Geek20"));
hs.add(new Employee(35, "Geek5"));
// create an iterator
Iterator<Employee> i = hs.iterator();
// iterate through the HashSet and
// print each employee object
while (i.hasNext()) {
System.out.println("Value: " + i.next() + " ");
}
}
}
// create a class representing Employee
class Employee {
int id;
String name;
Employee(int id, String name)
{
this.id = id;
this.name = name;
}
// Override the toString() to provide the string
// representation of the Employee object
public String toString()
{
return "[ " + this.id + ", " + this.name
+ " ]";
}
}
Output
Value: [ 35, Geek5 ] Value: [ 10, Geek1 ] Value: [ 2, Geek20 ]