A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/hashset-iterator-method-in-java/ below:

Java HashSet iterator() Method - GeeksforGeeks

Java HashSet iterator() Method

Last Updated : 11 Jul, 2025

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
// 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
// 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
            + " ]";
    }
}

Output
Value: [ 35, Geek5 ] 
Value: [ 10, Geek1 ] 
Value: [ 2, Geek20 ] 


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4