This example shows how to get the first element or last element from HashSet in Java. The example also shows how to get the first or last element from HashSet using various ways.
How to get the first or last element from HashSet in Java?
The HashSet class in Java does not make any guarantees to return the elements in the same order in which they were inserted. It means that element order may not remain constant over time. It also means that the iterator of the HashSet may return elements in a different order.
So technically there is no first or last HashSet element as the order in which the elements were returned from the HashSet iterator is not constant.
However, if you want to retrieve the element that is returned first or last by the HashSet iterator, you can do so using below given ways.
How to get the first element returned by the HashSet iterator?
To get the first element from HashSet returned by the iterator, you can write below given code.
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 |
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class HashSetGetFirstLastElementsExample { public static void main(String[] args) { Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); System.out.println("HashSet contains: " + hSetOddNumbers); //get an iterator Iterator<String> iterator = hSetOddNumbers.iterator(); if(iterator.hasNext()){ System.out.println( "First element is: " + iterator.next() ); }else{ System.out.println("HashSet is empty"); } } } |
Output
1 2 |
HashSet contains: [Five, One, Three] First element is: Five |
As you can see from the output, the HashSet iterator returned “Five” as the first element but it was added last to the HashSet.
Using Java 8
If you are using Java version 8 or later, you can also use the below given code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); if(!hSetOddNumbers.isEmpty()){ String firstElement = null; try{ //if first element is null, it will throw NullPointerException firstElement = hSetOddNumbers.stream().findFirst().get(); }catch(NullPointerException npe){ //do nothing, we already have default null assigned to firstElement } System.out.println("First: " + firstElement); }else{ System.out.println("HashSet is empty"); } |
Output
1 |
First: Five |
I personally like the Iterator approach just because of the simplicity of it.
Using an array
Instead of using an iterator, you can also convert HashSet to an array and then access the first element of an array that is located at index 0 as given below. This approach is not recommended as it needs to allocate a new array, iterate over all elements of the HashSet and copy them to an array.
It may slow down your code and not perform well if the HashSet has a large number of elements. I am providing the code below just for the demonstration purpose.
1 2 3 4 5 6 7 8 9 10 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); String[] strArray = hSetOddNumbers.toArray(new String[hSetOddNumbers.size()]); if(strArray.length > 0) System.out.println("First element: " + strArray[0]); |
Output
1 |
First element: Five |
How to get the last element returned by the HashSet iterator?
Iterate over all the elements of the HashSet to get the last element 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<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); System.out.println("HashSet contains: " + hSetOddNumbers); String lastElement = null; if(hSetOddNumbers.size() > 0){ //get an iterator Iterator<String> iterator = hSetOddNumbers.iterator(); while(iterator.hasNext()){ lastElement = iterator.next(); } System.out.println("Last element: " + lastElement); }else{ System.out.println("HashSet is empty"); } |
Output
1 2 |
HashSet contains: [Five, One, Three] Last element: Three |
While this is not recommended, we can also get the last element using the array just like we got the first as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); String[] strArray = hSetOddNumbers.toArray(new String[hSetOddNumbers.size()]); if(strArray.length > 0) System.out.println("Last element: " + strArray[ strArray.length - 1 ]); else System.out.println("HashSet is empty"); |
Output
1 |
Last element: Three |
Tip: If you want an ordered set, use the LinkedHashSet or TreeSet class 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