This example shows how to get LinkedHashSet elements by index in Java. This example also shows how to get elements by index from LinkedHashSet using an array, ArrayList, and iterator.
How to get elements by index from LinkedHashSet in Java?
The LinkedHashSet in Java is a hash table and linked list implementation of the Set interface. The LinkedHashSet class maintains a doubly-linked list running through elements but does not expose any methods using which we can get the elements using the index. However, if you still want you can access the elements by index as given below.
1. By converting to an array
We can convert LinkedHashSet to an array and then we can access the elements from the array 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 |
import java.util.LinkedHashSet; public class LinkedHashSetGetByIndexExample { 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); System.out.println("LinkedHashSet contains: " + lhSetEvenNumbers); //convert LinkedHashSet to an array Integer[] intArray = new Integer[ lhSetEvenNumbers.size() ]; intArray = lhSetEvenNumbers.toArray( intArray ); //get elements by index System.out.println("Element at index 0: " + intArray[0]); System.out.println("Element at index 2: " + intArray[2]); System.out.println("Element at index 4: " + intArray[4]); } } |
Output
1 2 3 4 |
LinkedHashSet contains: [2, 4, 6, 8, 10] Element at index 0: 2 Element at index 2: 6 Element at index 4: 10 |
Note: This approach needs the allocation of a new array with the linked hash set elements. If the LinkedHashSet contains large number of elements, it may not perform well. Plus, you need to convert to array every time there is a change in the set object. Use this only if the set is not changed much and you need to access large number of elements using the index.
2. By converting to a List
Instead of an array, we can also convert the LinkedHashSet to a List like ArrayList or LinkedList object and then we can access the elements from the List using the index and get method.
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); System.out.println("LinkedHashSet contains: " + lhSetEvenNumbers); //convert LinkedHashSet to List List<Integer> listNumbers = new ArrayList<Integer>( lhSetEvenNumbers ); //access elements by index and the get method System.out.println("Element at index 0: " + listNumbers.get(0)); System.out.println("Element at index 2: " + listNumbers.get(2)); System.out.println("Element at index 4: " + listNumbers.get(4)); |
Output
1 2 3 4 |
LinkedHashSet contains: [2, 4, 6, 8, 10] Element at index 0: 2 Element at index 2: 6 Element at index 4: 10 |
Note: Similar to the array approach, this needs a linked hash set object to be converted to the List which is a costly operation and may not perform well especially if the LinkedHashSet is very big. Only use this approach if you need to access large number of elements using the index and set is not changed much.
3. By iterating the Set
We can also iterate the LinkedHashSet elements while maintaining an index counter and when we reach the desired index, we can get that element as shown 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 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); System.out.println("LinkedHashSet contains: " + lhSetEvenNumbers); //get an iterator Iterator<Integer> itr = lhSetEvenNumbers.iterator(); int currentIndex = 0; int desiredIndex = 2; Integer element = null; while(itr.hasNext()){ element = itr.next(); if(currentIndex == desiredIndex){ System.out.println("Element at index " + desiredIndex + " is: " + element); break; } currentIndex++; } |
Output
1 2 |
LinkedHashSet contains: [2, 4, 6, 8, 10] Element at index 2 is: 6 |
Instead of an iterator, we can also use the 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 21 22 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); System.out.println("LinkedHashSet contains: " + lhSetEvenNumbers); int currentIndex = 0; int desiredIndex = 3; for(Integer number : lhSetEvenNumbers){ if(currentIndex == desiredIndex){ System.out.println("Element at index " + desiredIndex + " is: " + number); break; } currentIndex++; } |
Output
1 2 |
LinkedHashSet contains: [2, 4, 6, 8, 10] Element at index 3 is: 8 |
Important Note:
If the requirement is to access a very large number of elements by their indices, LinkedHashSet may not be a proper collection to use. Check if you can use the List based implementations like ArrayList or a LinkedList instead of the LinkedHashSet.
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