How to Get TreeMap Key or Value using Index in Java?

23 Jul, 2021
The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. 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.
 There are three simple ways to get key or TreeMap value using index in Java, which is the following :
Using an ArrayUsing a ListUsing iteration
Using an Array
Using a List
Using iteration
Method 1: Using an Array
We can get a TreeMap key or TreeMap value using an index in Java by using an Array. The process is divided into three steps:
Use the entrySet() method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object.Convert the entry set to an array using the toArray() method.And get TreeMap key or TreeMap value using index with the help of getKey() and getValue() method
Use the entrySet() method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object.
Convert the entry set to an array using the toArray() method.
And get TreeMap key or TreeMap value using index with the help of getKey() and getValue() method
Syntax:
Set<Map.Entry<Integer, String>> entrySet = treeMap.entrySet();
Map.Entry<Integer, String>[] entryArray = entrySet.toArray(new Map.Entry[entrySet.size()]);
Example:
Java
// Java Program to get TreeMap key or TreeMap value// using index // Importing all classes of// java.util packageimport java.util.*; // Classpublic class GFG {     // MAin driver method    public static void main(String[] args)    {         // Creating a New TreeMap        TreeMap<Integer, String> treeMap            = new TreeMap<Integer, String>();         // Add elements to TreeMap        // Custom inputs        treeMap.put(1, "Welcome");        treeMap.put(2, "geeks");        treeMap.put(3, "on");        treeMap.put(4, "geeksforgeeks");         // Get entry set of the TreeMap using entrySet        // method        Set<Map.Entry<Integer, String> > entrySet            = treeMap.entrySet();         // Convert entrySet to Array using toArray method        Map.Entry<Integer, String>[] entryArray            = entrySet.toArray(                new Map.Entry[entrySet.size()]);         // For loop for iteration  and printing        for (int i = 0; i < 4; i++)        {            // Get Key using index and print            System.out.println("Key at " + i + ":"                               + entryArray[i].getKey());             // Get value using index and print            System.out.println("Value at " + i + ":"                               + entryArray[i].getValue());        }             }}
Output:
1
Welcome
2
geeks
3
on
4
geeksforgeeks
 
Method 2: Using a List
We can get TreeMap key or TreeMap value using index in Java by using a List instead of Array. The process is divided into three steps:
Using entrySet() method of the TreeMap class to get a set view of all the entries stored in the TreeMap object.Now, converting the entry set to an array using the toArray() method.Finally, getting TreeMap key or TreeMap value using index with the help of get(), getKey() and getValue() methods.
Using entrySet() method of the TreeMap class to get a set view of all the entries stored in the TreeMap object.
Now, converting the entry set to an array using the toArray() method.
Finally, getting TreeMap key or TreeMap value using index with the help of get(), getKey() and getValue() methods.
 
Syntax: 
Set<Map.Entry<Integer, String>> entrySet = treeMap.entrySet();
List<Map.Entry<Integer, String>> entryList = new ArrayList<Map.Entry<Integer, String>>(entrySet);
Example:
Java
// Java Program to get TreeMap key or TreeMap value// using index // Importing all classes of// java.util packageimport java.util.*; // Classpublic class GFG {     // Main driver method    public static void main(String[] args)    {         // Creating a TreeMap        TreeMap<Integer, String> treeMap = new TreeMap<>();         // Add elements to TreeMap        // Custom inputs        treeMap.put(1, "Welcome");        treeMap.put(2, "geeks");        treeMap.put(3, "on");        treeMap.put(4, "geeksforgeeks");         // Get entry set of the TreeMap        // using entrySet method        Set<Map.Entry<Integer, String> > entrySet            = treeMap.entrySet();         // Converting entrySet to ArrayList        List<Map.Entry<Integer, String> > entryList            = new ArrayList<>(entrySet);         // For each loop for iteration        for (int i = 0; i < 4; i++) {             // Print Key and Values using index             // Get Key using index            System.out.println("Key at " + i + ":"                               + entryList.get(i).getKey());             // Get value using index            System.out.println(                "Value at " + i + ":"                + entryList.get(i).getValue());        }    }}
  
Key at 0:1
Value at 0:Welcome
Key at 1:2
Value at 1:geeks
Key at 2:3
Value at 2:on
Key at 3:4
Value at 3:geeksforgeeks
 Method 3: Using iteration
We can get TreeMap key or TreeMap value using index in Java by using an iteration. The process is divided into two steps: 
Using the entrySet() method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object.Iterating on entrySet to get TreeMap key or TreeMap value using index with the help of getKey() and getValue() methods.
Using the entrySet() method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object.
Iterating on entrySet to get TreeMap key or TreeMap value using index with the help of getKey() and getValue() methods.
 
Syntax:  
Set<Map.Entry<Integer, String>> entrySet = treeMap.entrySet();
Example:
Java
// Java Program to get TreeMap key or TreeMap value using// index // Importing all classes of// java.util packageimport java.util.*; // Classpublic class GFG {     // MAin driver method    public static void main(String[] args)    {        // Creating a TreeMp        TreeMap<Integer, String> treeMap = new TreeMap<>();         // Add elements to TreeMap        // Custom inputs        treeMap.put(1, "Welcome");        treeMap.put(2, "geeks");        treeMap.put(3, "on");        treeMap.put(4, "geeksforgeeks");         // Get entry set of the TreeMap        // using entrySet method        Set<Map.Entry<Integer, String> > entrySet            = treeMap.entrySet();         int index = 0;         // For-each loop for iteration        for (Map.Entry<Integer, String> currentEntry :             entrySet) {             // Print Key and Values using index             // Get Key using index            System.out.println("Key at " + index + ":"                               + currentEntry.getKey());             // Get value using index            System.out.println("Value at " + index + ":"                               + currentEntry.getValue());            index++;        }    }}
Key at 0:1
Value at 0:Welcome
Key at 1:2
Value at 1:geeks
Key at 2:3
Value at 2:on
Key at 3:4
Value at 3:geeksforgeeks
 
rajeev0719singh
java-TreeMap
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
List Interface in Java with Examples
Different ways of Reading a text file in Java
Constructors in Java
Strings in Java
HashMap containsKey() Method in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Traverse Through a HashMap in Java
Extends vs Implements in Java