How to Convert TreeMap to an ArrayList in Java?
Last Updated :
19 Sep, 2022
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();
}
}
OutputThe 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();
}
}
OutputThe 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]
Similar Reads
How to Convert a String to ArrayList in Java?
Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet
1 min read
Convert ArrayList to Vector in Java
There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t
3 min read
How to Convert ArrayList to HashSet in Java?
ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera
3 min read
How to Convert an ArrayList or an Array into a TreeSet in Java?
In Java, ArrayList allows random access because it works like an array. It means you can access any element by its index in O(1) at any time. A TreeSet is a sorted set implementation of the Set interface based on a TreeMap. It provides log(n) time complexity for basic operations like add, remove, an
3 min read
How to Convert an ArrayList into a JSON String in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON str
2 min read
Convert HashSet to a ArrayList in Java
ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can b
4 min read
How to Convert ArrayList to LinkedHashSet in Java?
ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th
8 min read
How to Convert an ArrayList of Objects to a JSON Array in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Obje
3 min read
How to Convert JSON Array to String Array in Java?
JSON stands for JavaScript Object Notation. It is one of the widely used formats to exchange data by web applications. JSON arrays are almost the same as arrays in JavaScript. They can be understood as a collection of data (strings, numbers, booleans) in an indexed manner. Given a JSON array, we wil
3 min read
How to Declare an ArrayList with Values in Java?
ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read