This example shows how to get elements by index from HashSet in Java. The example also shows how to get HashSet elements using an index using an iterator, for loop, array, and list.
How to get elements by index from HashSet in Java?
The HashSet is a collection of unique elements. The order of the elements returned by the HashSet iterator is not constant over time. The HashSet Java document has the below-given explanation for this behavior.
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
Plus, since the HashSet is backed by a HashMap, there are no methods to access its elements by index.
However, given all that, if you still want to access the elements by index, you can do so by below given ways.
1. Using an array
We can convert the HashSet object to an array and then access the elements using the index 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 24 25 26 27 28 |
import java.util.HashSet; import java.util.Set; public class HashSetElementsByIndexExample { public static void main(String[] args) { Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(10); hSetNumbers.add(20); hSetNumbers.add(30); hSetNumbers.add(40); hSetNumbers.add(50); System.out.println("HashSet contains: " + hSetNumbers); /* * Convert HashSet to an array */ Integer[] array = hSetNumbers.toArray( new Integer[hSetNumbers.size()] ); //access elements of an array using index for(int i = 0; i < array.length; i++){ System.out.println("Element at index " + i + " is:" + array[i]); } } } |
Output
1 2 3 4 5 6 |
HashSet contains: [50, 20, 40, 10, 30] Element at index 0 is:50 Element at index 1 is:20 Element at index 2 is:40 Element at index 3 is:10 Element at index 4 is:30 |
2. Using an ArrayList or LinkedList
Instead of an array, we can also convert the HashSet object to an ArrayList or a LinkedList and can then use the index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(10); hSetNumbers.add(20); hSetNumbers.add(30); hSetNumbers.add(40); hSetNumbers.add(50); /* * Convert Set to List */ List<Integer> list = new ArrayList<Integer>( hSetNumbers ); System.out.println("Element at index 0: " + list.get(0)); System.out.println("Element at index 1: " + list.get(1)); |
Output
1 2 |
Element at index 0: 50 Element at index 1: 20 |
3. Using an Iterator or a for loop
We can also iterate the HashSet using an Iterator or an enhanced for loop to get the element located at the desired location 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 |
Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(10); hSetNumbers.add(20); hSetNumbers.add(30); hSetNumbers.add(40); hSetNumbers.add(50); System.out.println("HashSet contains: " + hSetNumbers); int desiredIndex = 2; int currentIndex = 0; //iterate the HashSet Iterator<Integer> iterator = hSetNumbers.iterator(); while(iterator.hasNext()){ if(currentIndex == desiredIndex) System.out.println("Element at index " + desiredIndex + " is: " + iterator.next()); iterator.next(); currentIndex++; } |
Output
1 2 |
HashSet contains: [50, 20, 40, 10, 30] Element at index 2 is: 40 |
We can also use enhanced for loop as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(10); hSetNumbers.add(20); hSetNumbers.add(30); hSetNumbers.add(40); hSetNumbers.add(50); System.out.println("HashSet contains: " + hSetNumbers); int currentIndex = 0; for(Integer element : hSetNumbers){ if(currentIndex == 1){ System.out.println("Element at index 1 is: " + element); break; } currentIndex++; } |
Output
1 2 |
HashSet contains: [50, 20, 40, 10, 30] Element at index 1 is: 20 |
Important Note:
As we can see from the output above, even though we could get the elements by index, it is not in the order of the insertion. For example, we added element “10” first in the set, but in the output, we can see that it is at index 4. Plus, it is not even guaranteed that we will get the element “10” constantly at index 4.
The HashSet is not an appropriate choice of collection if you want to access the elements using an index. If your requirement is such, you should use any List implementation like LinkedList or ArrayList. If you want the Set functionality but want to maintain the insertion order of the elements, you can use the LinkedHashSet but then you will not be able to get elements by index.
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
Hi RahimV,
I want you to mentor me in my software development life as i am currently in third year pursuing CSE as my major.I face lot of difficulty when i implement some algorithms ,and the problem is i don’t have any human support and this results in too much waste of time to complete some courses.Therefore i request you to become my mentor as this will help me to become a good programmer in my career.
Hi Dhananjay,
What you call a waste of time is in fact the learning process. Everyone passes through that phase. That is the right way to learn, by making mistakes and learn from them. Mentoring is not feasible, nor required. Keep doing the hard work you are already doing and I am sure you will learn quite a lot.
Thanks and good luck.