Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Convert TreeMap to an ArrayList in Java?

Improve Article
Save Article
  • Last Updated : 19 Sep, 2022
Improve Article
Save Article

TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot have a null key but can have multiple null values. TreeMap is not synchronized, we have to synchronize it explicitly in order to use it in a multi-threading environment. TreeMap maintains the ascending order of the elements.

The way to convert TreeMap to ArrayList:

  • TreeMap having keys and values can be converted into two ArrayLists of keys and values.
  • Also, two ArrayLists, one with keys and the other with values, can be converted into a TreeMap.

Example:

TreeMap : {1=Welcome, 2=To, 3= Geeks, 4=For, 5=Geeks}
keyList : [1, 2, 3, 4, 5]
valueList : [Welcome, To, Geeks, For, Geeks]

Approach:

  • Create a TreeMap object and insert some keys and values.
  • Extract the keys from the TreeMap using TreeMap.keySet() method and put them into an ArrayList object which has been created for storing keys.
  • Extract the values from the TreeMap using TreeMap.values() method and put them into another ArrayList object which has been created for storing values.

Example:

Java




// Java program to demonstrate conversion of
// TreeMap to ArrayList
 
import java.util.*;
class GFG {
    // a class level treeMap object
    static TreeMap<Integer, String> treeMap
        = new TreeMap<Integer, String>();
 
    // Method to convert TreeMap to ArrayList
    static void convertMapToList()
    {
        // Extract the keys from the TreeMap
        // using TreeMap.keySet() and
        // assign them to keyList of type ArrayList
        ArrayList<Integer> keyList
            = new ArrayList<Integer>(treeMap.keySet());
 
        // Extract the values from the TreeMap
        // using TreeMap.values() and
        // assign them to valueList of type ArrayList
        ArrayList<String> valueList
            = new ArrayList<String>(treeMap.values());
 
        // printing the keyList
        System.out.println(
            "List of keys of the given Map : " + keyList);
 
        // printing the valueList
        System.out.println(
            "List of values of the given Map : "
            + valueList);
    }
 
    // Driver Method
    public static void main(String args[])
    {
        // inserting data into the TreeMap
        // using TreeMap.put() method
        treeMap.put(1, "Welcome");
        treeMap.put(2, "To");
        treeMap.put(3, "Geeks");
        treeMap.put(4, "For");
        treeMap.put(5, "Geeks");
 
        // printing the TreeMap
        System.out.println("The TreeMap is : " + treeMap);
 
        // calling convertMapToList() method
        convertMapToList();
    }
}


Output

The TreeMap is : {1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks}
List of keys of the given Map : [1, 2, 3, 4, 5]
List of values of the given Map : [Welcome, To, Geeks, For, Geeks]

Approach 2: Using entrySet() method 

In the below approach, we directly converted the entire TreeMap to ArrayList with both keys and values together. For this, we have to use Entry object. To use this approach, import the package java.util.Map.Entry. 

Here the keys and values in the TreeMap will directly get converted to ArrayList and the key and value for each index will be displayed as “key=value” in the ArrayList.

Step 1: Declare and add the values in the TreeMap.

Step 2: Call the function convertMaptoList().

Step 3: Declare the ArrayList with the Entry object in the function and specify the method TreeMap.entrySet() in the ArrayList constructor. It will convert the TreeMap values to ArrayList.

Step 4: Print the converted ArrayList.

Java




import java.util.*;
import java.util.Map.Entry;
 
public class GFG {
 
    static TreeMap<Integer, String> treeMap
        = new TreeMap<Integer, String>();
 
    static void convertMapToList()
    {
 
        List<Entry> keyList
            = new ArrayList<Entry>(treeMap.entrySet());
        System.out.println("Tree Map to ArrayList :"
                           + keyList);
    }
 
    public static void main(String[] args)
    {
        treeMap.put(1, "Welcome");
        treeMap.put(2, "To");
        treeMap.put(3, "Geeks");
        treeMap.put(4, "For");
        treeMap.put(5, "Geeks");
 
        // printing the TreeMap
        System.out.println("The TreeMap is : " + treeMap);
 
        // calling convertMapToList() method
        convertMapToList();
    }
}


Output

The TreeMap is : {1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks}
Tree Map to ArrayList :[1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks]

Approach 3 : Using addAll()

We can store the TreeMap object into list by using addAll(). For this, we have to import the package java.util.*. In this method, declare treemap_object.addAll(arraylist_object). It will copy and store the entire entryset to list.

Java




import java.util.*;
import java.util.Map.Entry;
 
public class GFG {
 
    static TreeMap<Integer, String> treeMap
        = new TreeMap<Integer, String>();
 
    static void convertMapToList()
    {
 
        List<Entry> keyList=new ArrayList<>();
      keyList.addAll(treeMap.entrySet());
        System.out.println("Tree Map to ArrayList :"
                           + keyList);
    }
 
    public static void main(String[] args)
    {
        treeMap.put(1, "Welcome");
        treeMap.put(2, "To");
        treeMap.put(3, "Geeks");
        treeMap.put(4, "For");
        treeMap.put(5, "Geeks");
 
        // printing the TreeMap
        System.out.println("The TreeMap is : " + treeMap);
 
        // calling convertMapToList() method
        convertMapToList();
    }
}


Output :

The TreeMap is : {1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks}
Tree Map to ArrayList :[1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks]


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!