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 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
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 package import java.util.*; Â
// Class public 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 , "neveropen" ); Â
        // 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 neveropen
Â
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.
Â
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 package import java.util.*; Â
// Class public 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 , "neveropen" ); Â
        // 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:neveropen
 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.
Â
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 package import java.util.*; Â
// Class public 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 , "neveropen" ); Â
        // 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:neveropen
Â