This example shows how to get the first or last element from LinkedHashSet in Java. This example also shows how to get the first element or the last element using an iterator, array, and Java 8 stream.
Even though the LinkedHashSet class maintains a doubly-linked list running through its elements, it does not expose any methods using which we can get LinkedHashSet elements using the index.
How to get the first element from the LinkedHashSet in Java?
1. Using an Iterator
We can get an iterator over the LinkedHashSet elements using the iterator
method. Once we get an iterator, we can iterate through the elements of the LinkedHashSet and get the first 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 24 |
import java.util.Iterator; import java.util.LinkedHashSet; public class LinkedHashSetGetFirstLastElement { public static void main(String[] args) { LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); //get an iterator Iterator<Integer> itr = lhSetEvenNumbers.iterator(); //get the first element if( itr.hasNext() ) System.out.println("First element: " + itr.next()); } } |
Output
1 |
First element: 2 |
2. Using Java 8 stream
If you are using Java version 8 or later, you can also use the stream to get the first 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 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); if(!lhSetEvenNumbers.isEmpty()){ Integer firstElement = null; try{ //if first element is null, it will throw NullPointerException firstElement = lhSetEvenNumbers.stream().findFirst().get(); }catch(NullPointerException npe){ //do nothing, we already have default null assigned to firstElement } System.out.println("First element: " + firstElement); } |
Output
1 |
First element: 2 |
3. By converting it to an array or List
We can convert the LinkedHashSet to an array or a List (like ArrayList or LinkedList) object and then can access the first element using the index 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); if(!lhSetEvenNumbers.isEmpty()){ //convert LinkedHashSet to an array first Integer[] intArray = new Integer[ lhSetEvenNumbers.size() ]; intArray = lhSetEvenNumbers.toArray(intArray); //access first element using index 0 System.out.println("First element: " + intArray[0]); } |
Output
1 |
First element: 2 |
Note: This approach needs the LinkedHashSet to first converted to an array or a List object which is unnecessary just to access the element. If the LinkedHashSet contains a very large number of elements, it may slow down the code and impact the performance. Using this approach is generally not recommended.
How to get the last element from the LinkedHashSet in Java?
1. Using an Iterator
We can get the last element from LinkedHashSet in the same we got the first element given above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); if(!lhSetEvenNumbers.isEmpty()){ Integer lastElement = null; Iterator<Integer> itr = lhSetEvenNumbers.iterator(); while(itr.hasNext()){ lastElement = itr.next(); } System.out.println("Last element: " + lastElement); } |
Output
1 |
Last element: 10 |
2. By converting it to an array or List
We can also convert LinkedHashSet to an array or a List object and then access the last element using the index as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); if(!lhSetEvenNumbers.isEmpty()){ //convert LinkedHashSet to an array Integer[] intArray = new Integer[ lhSetEvenNumbers.size() ]; intArray = lhSetEvenNumbers.toArray(intArray); //get the last element using last index System.out.println("Last element: " + intArray[ intArray.length - 1 ]); } |
Output
1 |
Last element: 10 |
This approach creates a new array from the LinkedHashSet elements and it is a costly operation in terms of performance. It may not perform well especially if the LinkedHashSet has a very large number of elements.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet