How to Convert TreeMap to an ArrayList in Java?
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
import java.util.*;
class GFG {
static TreeMap<Integer, String> treeMap
= new TreeMap<Integer, String>();
static void convertMapToList()
{
ArrayList<Integer> keyList
= new ArrayList<Integer>(treeMap.keySet());
ArrayList<String> valueList
= new ArrayList<String>(treeMap.values());
System.out.println(
"List of keys of the given Map : " + keyList);
System.out.println(
"List of values of the given Map : "
+ valueList);
}
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" );
System.out.println( "The TreeMap is : " + treeMap);
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" );
System.out.println( "The TreeMap is : " + treeMap);
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" );
System.out.println( "The TreeMap is : " + treeMap);
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]
Last Updated :
19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...