Horje
How to Iterate Over a HashSet Without an Iterator in Java?

In Java, we can iterate over a HashSet without using an Iterator. For this, we need two methods. We can directly loop through the elements of the HashSet.

In this article, we will discuss the two methods to iterate over a HashSet without using Iterator.

Program to Iterate over a HashSet without an Iterator

1. Using For-Each loop

Syntax:

for (Datatype variable :  Set)  { 
           //Write code using variable 
}

In this method, we will iterate over the HashSet using forEach loop. Below is the implementation:

Java

// Java program to demonstrate the use of 
// HashSet to store and retrieve unique values
  
import java.io.*;
import java.util.HashSet;
  
// Class definition for GFG (GeeksforGeeks)
class GFG {
    // Main method
    public static void main(String[] args)
    {
        // Step 1: Create a HashSet
        HashSet<String> set = new HashSet<String>();
  
        // Step 2: Adding values to HashSet
        set.add("rust");
        set.add("python");
        set.add("java");
        set.add("javascript");
        set.add("c++");
  
        // Step 3: Print the output on the screen
        System.out.println("Values stored in HashSet:");
          
        // Step 4: Iterate through the HashSet and print each value
        for (String var : set) {
            System.out.println(var);
        }
    }
}

Output

rust
python
c++
java
javascript

Explanation of the above Program:

  • In the above program, we have initialized a HashSet named set.
  • By using for-each loop we iterate over the HashSet.

2. Using forEach method

We can use forEach method which is a simple way to iterate over a set. And it can be written in single line. It is similar to ForEach loop. The values which are present in the set is stored in random order so if we iterate over it, the values will come in the random order.

Syntax:

collection.forEach(element ->  {
// Write code using element
});

Below is the implementation of forEach Method:

Java

// Java program demonstrating the use of
// HashSet and the forEach method to print values
import java.io.*;
import java.util.HashSet;
   
class GFG {
    // Main method
    public static void main(String[] args)
    {
        // Step 1: Create a HashSet
        HashSet<String> set = new HashSet<String>();
        
        // Step 2: Adding values to HashSet
        set.add("rust");
        set.add("python");
        set.add("java");
        set.add("javascript");
        set.add("c++");
          
        // Step 3: Using forEach method to print values
        System.out.println("Values printed using forEach method:");
        set.forEach(var -> System.out.println(var));
    }
}

Output

rust
python
c++
java
javascript

Explanation of the above Program:

  • In the above program, we have initialized a HashSet named set.
  • By using forEach() method, we iterate over the HashSet.



Reffered: https://www.geeksforgeeks.org


Java

Related
How to Create a HashSet With a Predefined Capacity in Java? How to Create a HashSet With a Predefined Capacity in Java?
Validating IPv4 String in Java Validating IPv4 String in Java
How to Shuffle Characters in a String in Java? How to Shuffle Characters in a String in Java?
Create Thread-safe HashMap Without Using Collections.synchronizedMap in Java Create Thread-safe HashMap Without Using Collections.synchronizedMap in Java
Java Program to Find Occurrence of a Word Using Regex Java Program to Find Occurrence of a Word Using Regex

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14