Open In App

Creating HashMap from Other Maps in Java

Last Updated : 15 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore, it behaves a bit differently from the rest of the collection types. A map contains unique keys.

There are three main types of maps in java

  1. HashMap
  2. LinkedHashMap
  3. TreeMap

These interfaces extend the Map interface. 

There are various ways to convert one map to another:

  1. Using iterator/a loop
  2. Using constructor
  3. Using putAll() method

Method 1: Using iterator/a loop

Iterate each element of the map (LinkedHashMap in this case) and add each one of them in the new HashMap.

Java




// Java program for creating HashMap from Other Maps
 
import java.util.*;
 
public class to_hashmap {
 
    public static void main(String a[])
    {
        // create an instance of LinkedHashMap
        LinkedHashMap<String, String> lhm
               = new LinkedHashMap<String, String>();
 
        // Add mappings using put method
        lhm.put("Apurva", "Bhatt");
        lhm.put("James", "Bond");
        lhm.put("Scarlett ", "Johansson");
 
        // It prints the elements in same order
        // as they were inserted
        System.out.println(lhm);
 
        Map<String, String> gfg = new HashMap<String, String>();
 
        // Using entrySet() method create a set out of the same elements
        // contained in the hash map
        for (Map.Entry<String, String> entry : lhm.entrySet())
            gfg.put(entry.getKey(), entry.getValue());
 
        System.out.println(gfg);
    }
}


Output

{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{James=Bond, Apurva=Bhatt, Scarlett =Johansson}

Method 2: Using constructor 

Pass the given Map (TreeMap in this case) to the HashMap constructor–it will automatically take care of converting the given Map to HashMap.

Java




// Java program for creating HashMap from Other Maps
// using constructor
 
import java.util.*;
 
public class to_hashmap {
 
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map<String, String> tm = new TreeMap<String, String>();
 
        // Add mappings using put method
        tm.put("Apurva", "Bhatt");
        tm.put("James", "Bond");
        tm.put("Scarlett ", "Johansson");
 
        // It prints the elements in same order
        // as they were inserted
        System.out.println(tm);
 
        Map<String, String> gfg = new HashMap<String, String>(tm);
 
        System.out.println(gfg);
    }
}


Output

{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}

Method 3: Using putAll() method

It is similar to the previous method, rather than passing the given Map to HashMap constructor, pass it to the putAll() method and it will automatically convert it to the HashMap.

Java




// Java program for creating HashMap from Other Maps
// using putAll() method
 
import java.util.*;
 
public class two_hashmap {
 
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map<String, String> tm = new TreeMap<String, String>();
 
        // Add mappings using put method
        tm.put("Apurva", "Bhatt");
        tm.put("James", "Bond");
        tm.put("Scarlett ", "Johansson");
 
        // It prints the elements in same order
        // as they were inserted
        System.out.println(tm);
 
        Map<String, String> gfg = new HashMap<String, String>();
 
        // using put all command
        gfg.putAll(tm);
 
        System.out.println(gfg);
    }
}


Output

{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads