How to convert a HashMap to TreeMap in Java

Hello programmers, in this tutorial you will learn how to convert a HashMap to TreeMap in Java. In this tutorial, you will be mainly using the putAll() method for converting and storing the HashMap entries into TreeMap.

The Difference Between HashMap and TreeMap

A HashMap basically contains unique elements but it does not maintain the order of insertion while a TreeMap also contains unique elements but instead, it maintains the order. Both HashMap and TreeMap can have multiple null values but only HashMap can contain a null key.

Java program to convert a HashMap to TreeMap

In the Java program first, you need to import two packages that are import java.util.*; and import java.util.stream.*;

After that, you need to create two methods in this class. These two classes are:

1)The main method: public static void main(String args[]) {}

2)The convertHmToTm method: public static <K, V> Map<K, V> convertHmToTm(Map<K, V> hm){}

The main method

  • In the main method first, you need to create a new HashMap and name its object as hm that will be containing both the key-value pair as String type.
Map<String, String> hm = new HashMap<>();
  • Then you need to use the put() method to add key-value pair entries in the HashMap. After adding the elements in the HashMap you need to print the HashMap. (The put() method is used to insert mapping into in the HashMap and this mapping is of type key-value pair).
hm.put("1", "Java"); 

hm.put("2", "Language"); 

hm.put("3", "Is a popular"); 

hm.put("4", "Programming language");

System.out.println("HashMap: " + hm);
  • Now, you need to call the “convertHmToTm” method by passing the HashMap entries into its arguments and store this in a variable. So first you need to create a new Map object for TreeMap of <String, String> type, and then name that variable as tm. In this tm variable, you are going to store the “convertHmToTm” method returned entry values/mappings.
Map<String, String> tm = convertHmToTm(hm);
  • After getting the HashMap mappings converted into treeMap you need to print the object by using a print statement.
 System.out.println("TreeMap: " + tm);

CODE:

    Map<String, String> hm = new HashMap<>(); 
 
    hm.put("1", "Java"); 
    hm.put("2", "Language"); 
    hm.put("3", "Is a popular"); 
    hm.put("4", "Programming language");	

    System.out.println("HashMap: " + hm); 
  
    Map<String, String> tm = convertHmToTm(hm); 
  
    System.out.println("TreeMap: " + tm); 
}

The convertHmToTm method

This method accepts one parameter i.e., Map. The HashMap is being sent from the main method and the convertHmToTm method returns Map that is the treeMap to the main method.

  • In this method first, you need to create a new Map of key-value pairs that is treeMAp.
Map<K, V> treeMap = new TreeMap<>();
  • Then, you need to write the object of treeMap with the “.” dot operator and write putAll() method with it. And pass the HashMap mappings object hm into it.
  • This putAll() method is basically used to copy all the entries from HashMap to treeMap. It selects all the key-value entry pairs from HashMap and then stores it into treeMap.
treeMap.putAll(hm);
  • After that, you need to return the treeMap object.
treeMap.putAll(hm);
  • In conclusion, HashMap entries or mappings will be now converted and stored into treeMap using just a putAll() method that took the HashMap values and stored it into treeMap.

CODE:

public class HashToTree{ 
  
    public static <K, V> Map<K, V> convertHmToTm(Map<K, V> hm) 
    { 
      
        Map<K, V> treeMap = new TreeMap<>(); 
  
        treeMap.putAll(hm); 
    
        return treeMap; 
    }

Convert a HashMap to TreeMap in Java

import java.util.*; 
import java.util.stream.*; 
  
public class HashToTree{ 
  
    public static <K, V> Map<K, V> convertHmToTm(Map<K, V> hm) 
    { 
      
        Map<K, V> treeMap = new TreeMap<>(); 
  
        treeMap.putAll(hm); 
    
        return treeMap; 
    } 
  
    public static void main(String args[]) 
    { 

        Map<String, String> hm = new HashMap<>(); 
     
        hm.put("1", "Java"); 
        hm.put("2", "Language"); 
        hm.put("3", "Is a popular"); 
        hm.put("4", "Programming language");	

        System.out.println("HashMap: " + hm); 
  
        Map<String, String> tm = convertHmToTm(hm); 
  
        System.out.println("TreeMap: " + tm); 
    } 
}

OUTPUT:

HashMap: {1=Java, 2=Language, 3=Is a popular, 4=Programming language}

TreeMap: {1=Java, 2=Language, 3=Is a popular, 4=Programming language}

Also, read: Java Program to remove odd frequency characters in a string

EnumSet in Java

How to clone a map in Java

Leave a Reply

Your email address will not be published. Required fields are marked *