How to traverse through a HashSet in Java

In this post, we will learn how to traverse through a HashSet in Java.
HashSet is one of the most efficient data structures in Java.
Here in this post, we will be learning how to traverse through them.

Traverse a HashSet in Java

What is a HashSet?

A HashSet in Java is used to create a collection of elements, it stores elements based on a unique process called Hashing.
The unique feature of a HashSet is that. A HashSet does not allow any duplicate elements to be stored in it.

In Java, we can create a HashSet as follows

HashSet<Integer> hashset = new HashSet<Integer>();

The above line of code creates a HashSet for us, of type Integer.
While creating a HashSet in Java, it is mandatory to specify the type.

We can add elements into our HashSet by the in-built add() methods as follows.

HashSet<Integer> hashset = new HashSet<Integer>();

hashset.add(1);
hashset.add(2); 
hashset.add(4);
hashset.add(10);

The above snippet will create a HashSet for us and contains the elements 1,2,4 and 10.
Note that the HashSet doesn’t store elements based on insertion order instead is uses Hashing.

 

A HashSet has the following properties.

  1. HashSet contains only unique elements.
  2. HashSet does not contain any particular insertion order, it stores based on hashing.
  3. A HashSet takes almost constant time in search operations.

There are a lot of methods and operations on HashSets, you can learn more about HashSets here:

How to traverse through a HashSet 

There are multiple ways of accessing or iterating a HashSet in Java, we’ll be discussing the most efficient and used ones here.

Using the Java Iterator class.

Iterators are derived from the Java Collections framework and they are used to access elements of any Collection Object linearly i.e, one by one.
Iterators do not have any indexing so to print elements we use the iterator.next() method which will give us the next element in our collection object.
Iterators can be used on other Collection objects as well like a Deque, ArrayList, List, and so on.
In Java, we can create an iterator as follows.

Iterator<String> iterator = mySet.iterator();

The above line of code will create an iterator for a collection object named mySet and the data type of that collection object is <String> .
So whenever we create an iterator we have to specify the data type and the name of the collection object on which the iterator will be used.

import java.util.*; 
  
class HashSetIterator { 
    public static void main(String[] args) 
    { 
        //Create a HashSet
        HashSet<Integer> hashset = new HashSet<Integer>(); 
  
       //Add some elements
       hashset.add(1);
       hashset.add(2);
       hashset.add(4);
       hashset.add(10);
  
        //Lets iterate over it
        Iterator<Integer> iterator = hashset.iterator(); 
        
        //hasNext() function Lets
        //us know if there are more elements
        //present in the collection object
        while (iterator.hasNext()){ 
            System.out.println(iterator.next()); 
        }
    } 
}

Output

1
2
4
10

 

Using a For-Each loop

For-Each loops are another way to traverse a collection object, like an iterator a For-Each loop only moves forward and can only be used to access the elements we cannot modify any elements.

Similar to iterators, the For-Each loop can also be used on various Java collection objects like Lists, Sets, and so on.

The syntax of the For-Each loop is almost similar to the normal for loops, the exception is that here we do not have any
condition or updation statements.

In Java, the syntax of the For-Each loop is as follows

for( type of data : Collection object){
 //Code
}

The For-Each loop requires the type of data that is going to be accessed like Integer, String etc., and the Collection object which will be traversed, can be a Set or a Lists or Deque and so on.

import java.util.*; 
  
class CodeChef { 
    public static void main(String[] args) 
    { 
        //Create a HashSet
        HashSet<Integer> hashset = new HashSet<Integer>(); 
  
       //Add some elements
       hashset.add(1);
       hashset.add(2);
       hashset.add(4);
       hashset.add(10);
        
       //Using the For-Each loop
       for(Integer i:hashset){
           System.out.println(i);
       }
    }
}

Output

1
2
4
10

Also read:

Leave a Reply