This example shows how to iterate HashSet in Java. This example also shows how to iterate Java HashSet using for loop, HashSet iterator, and forEach.
How to iterate HashSet in Java?
The Set is not an index based collection hence the HashSet class does not provide any methods to get elements by their index. For example, the ArrayList get method accepts the index argument and returns an element located at the specified index.
Since there is no method to get the HashSet elements by index, we have to iterate over the HashSet to get elements from it.
There are several ways using which we can iterate over elements of the HashSet object in Java as given below.
1. Iterate using for loop
We can use the enhanced for loop to iterate through the elements of the HashSet object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashSet; import java.util.Set; public class JavaHashSetIterateExample { public static void main(String[] args) { Set<String> hSetEvenNumbers = new HashSet<String>(); hSetEvenNumbers.add("Two"); hSetEvenNumbers.add("Four"); hSetEvenNumbers.add("Six"); hSetEvenNumbers.add("Eight"); /* * Iterate using enhanced for loop */ for( String strNumber : hSetEvenNumbers ){ System.out.println( strNumber ); } } } |
Output
1 2 3 4 |
Eight Six Four Two |
2. Iterate using an Iterator
The iterator
method of the HashSet class returns an iterator object over the elements of this set object.
1 |
public Iterator<E> iterator() |
Once we get the iterator object using the HashSet iterator
method, we can iterate through elements of the set using the hasNext
and next
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Set<String> hSetEvenNumbers = new HashSet<String>(); hSetEvenNumbers.add("Two"); hSetEvenNumbers.add("Four"); hSetEvenNumbers.add("Six"); hSetEvenNumbers.add("Eight"); /* * Iterate over elements of HashSet using an Iterator */ //get the Iterator Iterator<String> iterator = hSetEvenNumbers.iterator(); //iterate using hasNext and next methods while( iterator.hasNext() ){ System.out.println( iterator.next() ); } |
Output
1 2 3 4 |
Eight Six Four Two |
3. Iterate using forEach
If you are using Java version 8 or later, you can also use the forEach to iterate over the elements as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Set<String> hSetEvenNumbers = new HashSet<String>(); hSetEvenNumbers.add("Two"); hSetEvenNumbers.add("Four"); hSetEvenNumbers.add("Six"); hSetEvenNumbers.add("Eight"); /* * For Java version 8 and later only. * * Iterate over HashSet using forEach */ hSetEvenNumbers.forEach( element -> { System.out.println( element ); }); |
Output
1 2 3 4 |
Eight Six Four Two |
Important Note:
You may have observed that the order of the HashSet elements is not the same as the insertion order. For example, the element “Two” was inserted first, but in the output, it was printed last.
The reason is, the List is an ordered collection while the Set is not. It means that the List interface guarantees to return the elements in the same order in which they were inserted. However, the Set interface does not make any such guarantee. When you retrieve elements from the Set, they may be returned in a different order depending upon the type of Set implementation class.
The HashSet class does not make any guarantee the order. In other words, the order of the elements may not remain constant over time. If you want to use the Set but still want to maintain the insertion order of the elements, use the LinkedHashSet instead of the HashSet class.
This example is a part of the HashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet