This example shows how to get a value from the LinkedHashMap object using the index. The example also shows various ways to get the value using index from LinkedHashMap instead of the key.
How to get a value from LinkedHashMap by index in Java?
Even though the LinkedHashMap class is a linked list implementation of the Map interface, it looks more like a map than the linked list. The class has not exposed any methods using which you can access entries (key-value mappings) using the index. The reason is, like with any other regular map implementations, we are supposed to access the LinkedHashMap values using keys, not using index.
However, you can still do that if you want in below given ways.
1. Using keys array
You can get all keys of the LinkedHashMap using the keySet
method, convert the set to an array using the toArray
method, access the key using array index and then get the value for that key from the LinkedHashMap 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 29 30 |
import java.util.LinkedHashMap; import java.util.Set; public class LinkedHashMapGetValueByIndexExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); /* * Get all the keys */ Set<Integer> keySet = lhmap.keySet(); /* * Convert set to an array using the toArray method */ Integer[] keyArray = keySet.toArray( new Integer[ keySet.size() ] ); //get key for the specified index Integer key = keyArray[1]; //get value from the LinkedHashMap for the key System.out.println( "Value at index 1: " + lhmap.get(key) ); } } |
Output
1 |
Value at index 1: Two |
2. Using the List
Instead of converting to an array, you can also convert the key set to an ArrayList or LinkedList and then access the key using index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); /* * Get all the keys */ Set<Integer> keySet = lhmap.keySet(); /* * Convert set to an ArrayList */ List<Integer> listKeys = new ArrayList<Integer>( keySet ); //get the key for the desired index using get method Integer key = listKeys.get(0); //get value from the LinkedHashMap System.out.println( "Value at index 0: " + lhmap.get(key) ); |
Output
1 |
Value at index 0: One |
3. Using an Iterator
You can get all the entries from the LinkedHashMap using the entrySet
method, iterate through them using the hasNext
and next
method of the iterator while maintaining the index counter and get the value for the desired 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 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); //get all entries from the LinkedHashMap Set<Map.Entry<Integer, String>> entrySet = lhmap.entrySet(); //get an iterator Iterator<Map.Entry<Integer, String>> iterator = entrySet.iterator(); int i = 0; int index = 1; String value = null; while(iterator.hasNext()){ if(index == i){ value = iterator.next().getValue(); break; } iterator.next(); i++; } System.out.println("Value at index 1: " + value); |
Output
1 |
Value at index 1: Two |
Important Note:
None of the above approaches should be used as they do not perform well especially if the map is large. All of them require iterating through the LinkedHashMap directly (approach 3) or indirectly (approaches 1 and 2). Plus, approaches 1 and 2 requires allocating a new array or ArrayList to perform this task which is a costly operation.
If you really need to access the values using index instead of the keys, then I think you should use other index-based collections (like List) instead of the LinkedHashMap.
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap