This example shows how to get TreeMap key or value using the index in Java. This example also shows various ways to get TreeMap key or value by index.
How to get TreeMap key or value using an index in Java?
The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using which we can access the TreeMap keys or values using their indices. However, if you still want you can do so using below given ways.
1. Using an array
Use the entrySet
method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object and then convert the entry set to an array using the toArray
method. Once you get an array, you can get the entries using their 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 29 30 31 32 |
import java.util.Map; import java.util.Set; import java.util.TreeMap; public class TreeMapGetKeyValueUsingIndexExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); tmapColors.put(4, "Yellow"); tmapColors.put(5, "Brown"); Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); /* * Convert entry set to an array using toArray method */ Map.Entry<Integer, String>[] entryArray = entries.toArray( new Map.Entry[entries.size()] ); //get key at index 2 System.out.println( "Key at index 2: " + entryArray[2].getKey() ); //get value at index 2 System.out.println( "Value at index 2: " +entryArray[2].getValue() ); } } |
Output
1 2 |
Key at index 2: 3 Value at index 2: Blue |
2. Using a List
Instead of converting the entry set to an array, you can convert it to the List (ArrayList or LinkedList) object and then access the TreeMap entries using the get
method of the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); tmapColors.put(4, "Yellow"); tmapColors.put(5, "Brown"); Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); /* * Convert entry set to list */ List<Map.Entry<Integer, String>> listEntries = new ArrayList<Map.Entry<Integer, String>>(entries); //get key at index 1 System.out.println( "Key at index 1: " + listEntries.get(1).getKey() ); //get value at index 1 System.out.println( "Value at index 1: " + listEntries.get(1).getValue() ); |
Output
1 2 |
Key at index 1: 2 Value at index 1: Green |
3. Using an Iterator
You can also iterate through TreeMap entries using an iterator and get the key or value located at 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 29 30 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); tmapColors.put(4, "Yellow"); tmapColors.put(5, "Brown"); Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); //get an iterator for the entries Iterator<Map.Entry<Integer, String>> iterator = entries.iterator(); int i = 0; int desiredIndex = 3; Map.Entry<Integer, String> currentEntry = null; while(iterator.hasNext()){ currentEntry = iterator.next(); if(i == desiredIndex){ System.out.println( "Key at index " + desiredIndex + " is: " + currentEntry.getKey() ); System.out.println( "Value at index " + desiredIndex + " is: " + currentEntry.getValue() ); break; } i++; } |
Output
1 2 |
Key at index 3 is: 4 Value at index 3 is: Yellow |
Important Notes:
1. All of the above given approaches iterates the TreeMap which may impact performance especially if the map is too large.
2. Converting the entry set to an array or to a List is unnecessary overhead and needs to allocate a new array or list that is costly operation in terms of performance.
3. Any map implementation is not supposed to be accessed by index. If you need to access the key or value using the index see if you can use any of the list implementations instead of the map.
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap