Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  1. Using an Array
  2. Using a List
  3. 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:

  1. Use the entrySet() method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object.
  2. Convert the entry set to an array using the toArray() method.
  3. 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, "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:

  1. Using entrySet() method of the TreeMap class to get a set view of all the entries stored in the TreeMap object.
  2. Now, converting the entry set to an array using the toArray() method.
  3. 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, "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());
        }
    }
}


 
 

Output

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: 

  1. Using the entrySet() method of the TreeMap class to get a Set view of all the entries stored in the TreeMap object.
  2. 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, "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++;
        }
    }
}


Output

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

 



Last Updated : 23 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads