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
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]
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
How to convert an Array to String in Java?
In Java, there are different ways to convert an array to a string. We can choose ways from built-in methods or custom approaches, depending on the type of arrays. Using Arrays.toString()The most simple method is the Arrays.toString() to convert an array to a string. Example: [GFGTABS] Java // Java P
1 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
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 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 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 an Array to LinkedHashSet in Java?
An array is a collection of items stored at contiguous memory locations. An array can contain primitives data types as well as objects of a class depending on the definition of the array. Whereas LinkedHashSet contains only unique elements and they are stored in the same order in which they are inse
3 min read
Convert ArrayList to HashMap in Java
Array List can be converted into HashMap, but the HashMap does not maintain the order of ArrayList. To maintain the order, we can use LinkedHashMap which is the implementation of HashMap. Basically, there are two different ways to convert ArrayList to Hashmap- Using ArrayList IterationUsing ArrayLis
3 min read