Wednesday, July 3, 2024
HomeLanguagesJavaHow to iterate HashSet in Java?

How to iterate HashSet in Java?

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments