This example shows how to convert TreeMap to ArrayList in Java. The example also shows how to convert TreeMap keys, values, or entries to an ArrayList.
How to convert TreeMap to ArrayList in Java?
There are several ways using which we can convert TreeMap keys, values or entries to ArrayList as given below.
1. Convert TreeMap keys to ArrayList
Get all the keys of the TreeMap object using the keySet
method. The keySet
method returns a Set view of all the keys. Once we have the key set, we can convert it to an ArrayList using the constructor as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeMap; public class TreeMapToArrayListExample { public static void main(String[] args) { TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * Get all keys using the keySet method */ Set<Integer> setKeys = treemapNumbers.keySet(); //convert it to a List using the constructor and pass the key set List<Integer> listKeys = new ArrayList<Integer>( setKeys ); System.out.println("List contains: " + listKeys ); } } |
Output
1 |
List contains: [1, 2, 3] |
2. TreeMap values to ArrayList
Get all the values of the TreeMap object using the values
method. The values
method returns a Collection view of all the values. Once we have the values collection, we can convert it to an ArrayList using the constructor as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * Get all values using the values method */ Collection<String> values = treemapNumbers.values(); /* * Convert it to the List using the ArrayList constructor */ List<String> listValues = new ArrayList<String>( values ); System.out.println("List contains: " + listValues); |
Output
1 |
List contains: [One, Two, Three] |
3. TreeMap entries to ArrayList
Get all the entries stored in the TreeMap using the entrySet
method. The entrySet
method returns a Set view of all the entries. Once we have the entry set, we can convert it to an ArrayList using the constructor as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * Get all entries using the entrySet method */ Set<Map.Entry<Integer, String>> entrySet = treemapNumbers.entrySet(); /* * Convert the entry set to ArrayList using constructor */ List<Map.Entry<Integer, String>> listEntries = new ArrayList<Map.Entry<Integer, String>>( entrySet ); //iterate the list for( Map.Entry<Integer, String> entry : listEntries ){ System.out.println( entry.getKey() + "->" + entry.getValue() ); } |
Output
1 2 3 |
1->One 2->Two 3->Three |
As you can see from the output, the entries are sorted automatically by the TreeSet class in the key’s natural order (that is ascending for the type Integer).
You can also convert keys, values, and entries to the LinkedList instead of an ArrayList using the same process.
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap