Convert HashMap to TreeMap Java example shows how to convert HashMap to TreeMap using a constructor, the putAll method and a custom Comparator.
How to convert HashMap to TreeMap in Java?
The HashMap object can be converted to the TreeMap object using below given ways.
1) Convert HashMap to TreeMap using the TreeMap constructor
You can use the below given TreeMap constructor to convert.
1 |
TreeMap(Map <? extends K, ? extends V> map) |
This constructor creates a TreeMap object containing the same mapping as an argument map. It also orders the elements according to the natural ordering of the map’s 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.javacodeexamples.collectionexamples; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import com.javacodeexamples.common.Student; public class ConvertHashMapToTreeMapExample { public static void main(String[] args) { /* * create a new HashMap to store * student's total marks and Student mapping */ HashMap<Integer, Student> hMapStudentMarks = new HashMap<Integer, Student>(); //add Student objects //add Student objects hMapStudentMarks.put( 76, new Student("S01", "Student1", "12th", 76) ); hMapStudentMarks.put( 57, new Student("S01", "Student1", "12th", 57) ); hMapStudentMarks.put( 88, new Student("S01", "Student1", "12th", 88) ); hMapStudentMarks.put( 65, new Student("S01", "Student1", "12th", 65) ); hMapStudentMarks.put( 46, new Student("S01", "Student1", "12th", 46) ); System.out.println("HashMap Keys and Values"); for(Map.Entry<Integer, Student> entry : hMapStudentMarks.entrySet()){ System.out.println(entry.getKey() + " => " + ": " + entry.getValue()); } //blank line System.out.println(""); /* * Use the TreeMap * constructor which accepts an existing Map * object */ TreeMap<Integer, Student> tMapStudentMarks = new TreeMap<Integer, Student>(hMapStudentMarks); System.out.println("TreeMap Keys and Values"); for(Map.Entry<Integer, Student> entry : tMapStudentMarks.entrySet()){ System.out.println(entry.getKey() + " => " + ": " + entry.getValue()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HashMap Keys and Values 65 => : [S01 : Student1 : 12th : 65] 76 => : [S01 : Student1 : 12th : 76] 57 => : [S01 : Student1 : 12th : 57] 46 => : [S01 : Student1 : 12th : 46] 88 => : [S01 : Student1 : 12th : 88] TreeMap Keys and Values 46 => : [S01 : Student1 : 12th : 46] 57 => : [S01 : Student1 : 12th : 57] 65 => : [S01 : Student1 : 12th : 65] 76 => : [S01 : Student1 : 12th : 76] 88 => : [S01 : Student1 : 12th : 88] |
You may have noticed that when we printed the TreeMap entries, they are sorted by key i.e. sorted by student’s total marks in ascending order. This is a very desirable side effect of converting the TreeMap. The TreeMap class by default orders the mappings according to the natural ordering of the keys.
2) Using the putAll method of the TreeMap
You can convert using the default constructor of the TreeMap along with the putAll
method.
1 |
void putAll(Map<? extends K, ? extends V> map) |
This method copies all the mapping from the specified map to the TreeMap. It also orders the keys of the Map according to the natural ordering.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.javacodeexamples.collectionexamples; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import com.javacodeexamples.common.Student; public class ConvertHashMapToTreeMapExample { public static void main(String[] args) { /* * create a new HashMap to store * student's total marks and Student mapping */ HashMap<Integer, Student> hMapStudentMarks = new HashMap<Integer, Student>(); //add Student objects hMapStudentMarks.put( 76, new Student("S01", "Student1", "12th", 76) ); hMapStudentMarks.put( 57, new Student("S01", "Student1", "12th", 57) ); hMapStudentMarks.put( 88, new Student("S01", "Student1", "12th", 88) ); hMapStudentMarks.put( 65, new Student("S01", "Student1", "12th", 65) ); hMapStudentMarks.put( 46, new Student("S01", "Student1", "12th", 46) ); System.out.println("HashMap Keys and Values"); for(Map.Entry<Integer, Student> entry : hMapStudentMarks.entrySet()){ System.out.println(entry.getKey() + " => " + ": " + entry.getValue()); } //blank line System.out.println(""); /* * You can also use default constructor of TreeMap * and later use putAll method of TreeMap class to * convert HashMap to TreeMap */ TreeMap<Integer, Student> tMapStudentMarks = new TreeMap<Integer, Student>(); tMapStudentMarks.putAll(hMapStudentMarks); System.out.println("TreeMap Keys and Values"); for(Map.Entry<Integer, Student> entry : tMapStudentMarks.entrySet()){ System.out.println(entry.getKey() + " => " + ": " + entry.getValue()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HashMap Keys and Values 65 => : [S01 : Student1 : 12th : 65] 76 => : [S01 : Student1 : 12th : 76] 57 => : [S01 : Student1 : 12th : 57] 46 => : [S01 : Student1 : 12th : 46] 88 => : [S01 : Student1 : 12th : 88] TreeMap Keys and Values 46 => : [S01 : Student1 : 12th : 46] 57 => : [S01 : Student1 : 12th : 57] 65 => : [S01 : Student1 : 12th : 65] 76 => : [S01 : Student1 : 12th : 76] 88 => : [S01 : Student1 : 12th : 88] |
How to provide custom Comparator while converting to the TreeMap?
If the HashMap keys are custom class objects and you want to sort it using custom Comparator while converting the TreeMap object, you can specify it using the TreeMap constructor as given below.
1 |
TreeMap(Comparator <? super K> comparator) |
This constructor creates an empty TreeMap object whose keys will be sorted according to the specified custom Comparator.
In below given example, we are going to sort the Student objects according to total marks in descending order.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package com.javacodeexamples.collectionexamples; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import com.javacodeexamples.common.Student; public class ConvertHashMapToTreeMapExample { public static void main(String[] args) { /* * create a new HashMap to store * student's total marks and Student mapping */ HashMap<Integer, Student> hMapStudentMarks = new HashMap<Integer, Student>(); //add Student objects hMapStudentMarks.put( 76, new Student("S01", "Student1", "12th", 76) ); hMapStudentMarks.put( 57, new Student("S01", "Student1", "12th", 57) ); hMapStudentMarks.put( 88, new Student("S01", "Student1", "12th", 88) ); hMapStudentMarks.put( 65, new Student("S01", "Student1", "12th", 65) ); hMapStudentMarks.put( 46, new Student("S01", "Student1", "12th", 46) ); System.out.println("HashMap Keys and Values"); for(Map.Entry<Integer, Student> entry : hMapStudentMarks.entrySet()){ System.out.println(entry.getKey() + " => " + ": " + entry.getValue()); } //blank line System.out.println(""); TreeMap<Integer, Student> tMapStudentMarks = new TreeMap<Integer, Student>( getStudentComparator() ); tMapStudentMarks.putAll(hMapStudentMarks); System.out.println("TreeMap Keys and Values"); for(Map.Entry<Integer, Student> entry : tMapStudentMarks.entrySet()){ System.out.println(entry.getKey() + " => " + ": " + entry.getValue()); } } /* * returns a custom comparator to sort Student * objects according to total marks in descending order */ private static Comparator<Integer> getStudentComparator(){ return new Comparator<Integer>(){ public int compare(Integer i1, Integer i2) { return i2.compareTo(i1); } }; } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HashMap Keys and Values 65 => : [S01 : Student1 : 12th : 65] 76 => : [S01 : Student1 : 12th : 76] 57 => : [S01 : Student1 : 12th : 57] 46 => : [S01 : Student1 : 12th : 46] 88 => : [S01 : Student1 : 12th : 88] TreeMap Keys and Values 88 => : [S01 : Student1 : 12th : 88] 76 => : [S01 : Student1 : 12th : 76] 65 => : [S01 : Student1 : 12th : 65] 57 => : [S01 : Student1 : 12th : 57] 46 => : [S01 : Student1 : 12th : 46] |
What is the preferred way to convert?
If you want to sort the TreeMap objects by a custom Comparator, then you will have to use the putAll
method. If you don’t, using the constructor that accepts the existing Map is the preferred way to convert as it produces cleaner code.
This example is a part of the Java HashMap tutorial with examples and Java TreeMap tutorial with examples.
Please let me know your views in the comments section below.