This example shows how to create a new TreeMap object using the TreeMap constructors in Java. This example also shows the use of various TreeMap constructors.
How to create new TreeMap objects in Java using TreeMap constructors?
The TreeMap class in Java provides several constructors using which we can create new objects of it.
1. Create new empty TreeMap object
The default constructor of the TreeMap class creates a new and empty map object.
1 |
public TreeMap() |
The map will use the natural ordering of its keys. The keys inserted into this map object must implement the Comparable interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.TreeMap; public class TreeMapConstructorsExample { public static void main(String[] args) { /* * This will create a new and empty TreeMap object whose keys * are of type Integer and values are of type String. * * The mappings of this TreeMap will be sorted automatically by * the Integer keys in ascending order */ TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(); //add mappings treeMap.put(3, "Three"); treeMap.put(1, "One"); treeMap.put(2, "Two"); System.out.println("TreeMap contains: " + treeMap); } } |
Output
1 |
TreeMap contains: {1=One, 2=Two, 3=Three} |
As you can see from the output, all the entries of the TreeMap are automatically sorted in the natural ordering of the keys (which is an ascending order for the type Integer).
2. Create new TreeMap with custom Comparator
As we have seen, the default constructor uses the natural ordering of the keys to sort the entries. In case you want to provide the custom order, there is an overloaded constructor that accepts a custom Comparator object.
1 |
public TreeMap(Comparator<? super K> comparator) |
This constructor creates an empty TreeMap object whose entries will be ordered according to the specified comparator object instead of the natural ordering of the keys.
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 27 28 29 30 31 32 33 34 |
import java.util.Comparator; import java.util.TreeMap; class DescendingComparator implements Comparator<Integer>{ /* * This will sort Integer objects in descending order */ public int compare(Integer i1, Integer i2) { return i2-i1; } } public class TreeMapConstructorsExample { public static void main(String[] args) { /* * This will create a new and empty TreeMap object whose keys * are of type Integer and values are of type String. * * The mappings of this TreeMap will be sorted automatically by * the specified comparator object */ TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>( new DescendingComparator() ); //add mappings treeMap.put(1, "One"); treeMap.put(2, "Two"); treeMap.put(3, "Three"); System.out.println("TreeMap contains: " + treeMap); } } |
Output
1 |
TreeMap contains: {3=Three, 2=Two, 1=One} |
3. Creating a TreeMap from another Map (HashMap, LinkedHashMap)
There is an overloaded constructor that accepts a Map object as an argument.
1 |
public TreeMap(Map<? extends K,? extends V> map) |
It creates a new TreeMap object having the same mappings as the specified map object. The keys of the specified map object must implement the Comparable interface for this to work or else the ClassCastException will be thrown. The mappings copied from the map will be automatically sorted by the TreeMap according to the natural order of the keys.
Convert HashMap to TreeMap Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HashMap<Integer, String> hmap = new HashMap<Integer, String>(); hmap.put(3, "Three"); hmap.put(2, "Two"); hmap.put(1, "One"); /* * To convert HashMap to TreeMap, use this * constructor and specify the map object */ TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>( hmap ); System.out.println("TreeMap contains: " + treeMap); |
Output
1 |
TreeMap contains: {1=One, 2=Two, 3=Three} |
As you can see from the output, all HashMap entries are automatically sorted in the TreeMap object.
This example is a part of the Java TreeMap Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap