Skip to content
Related Articles

Related Articles

How to Copy One HashMap to Another HashMap in Java?
  • Last Updated : 15 Dec, 2020

HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values.  This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.util.HashMap package or its superclass.

Given a HashMap, there are three ways one can copy the given HashMap to another:

  1. By normally iterating and putting it to another HashMap using put(k, v) method.
  2. Using putAll() method.
  3. Using copy constructor.

Method 1: By normally iterating and putting it to another HashMap using put(k, v) Method.

A simple solution is to iterate through the map and use put(Key,Value) once for each mapping key and value in the another Map.

Java




// Java program to iterate through the
// first map and put it in the second map
  
import java.util.HashMap;
import java.util.Map;
  
class GFG {
    public static <K, V> Map<K, V>
    copyMap(Map<K, V> original)
    {
  
        Map<K, V> second_Map = new HashMap<>();
  
        // Start the iteration and copy the Key and Value
        // for each Map to the other Map.
        for (Map.Entry<K, V> entry : original.entrySet()) {
  
            // using put method to copy one Map to Other
            second_Map.put(entry.getKey(),
                           entry.getValue());
        }
  
        return second_Map;
    }
  
    public static void main(String[] args)
    {
  
        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
          
        // copyMap method would copy the original 
        // hashMap to second_Map
        Map<String, Integer> second_Map = copyMap(hashMap);
        
        System.out.println(second_Map);
    }
}
Output



{A=1, B=2, C=3}

Method 2: Using putAll(k, v) Method.

Map.putAll(k,v) method is used to copy one HashMap to another empty HashMap.

Syntax:

new_hash_map.putAll(exist_hash_map)

Parameters: The method takes one parameter exist_hash_map that refers to the existing map we want to copy from.

Return Value: The method does not return any values.

Exception: The method throws NullPointerException if the map we want to copy from is NULL.

Java




// Java program to copy hashmap to
// another hashmap using putAll() method
  
import java.util.HashMap;
import java.util.Map;
  
class GFG {
    public static <K, V> Map<K, V>
    copyMap(Map<K, V> original)
    {
  
        Map<K, V> second_map = new HashMap<>();
  
        // using putAll method to copy from original Map to
        // second_map
        second_map.putAll(original);
  
        return second_map;
    }
  
    public static void main(String[] args)
    {
  
        Map<String, Integer> hashMap = new HashMap<>();
  
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
  
        // copyMap method would copy the original 
        // hashMap to second_Map
        Map<String, Integer> second_map = copyMap(hashMap);
        
        System.out.println(second_map);
    }
}
Output
{A=1, B=2, C=3}

Method 3: Using copy constructor.

It is one of the shortest and easiest ways to copy one HashMap to another.

We can use a copy constructor to copy a map which is a special constructor for creating a new object as a copy of an existing object.

Java




import java.util.HashMap;
import java.util.Map;
  
class GFG {
    
    // usimg copy constructor to resturn the original map
    // and then copy it in second_map
    public static <K, V> Map<K, V> copyMap(Map<K, V> original)
    {
       // constructor by passing original hashmap
       // in the parameter returns the new hashmap 
       // with the copied content of the original one
        return new HashMap<>(original);
    }
  
    public static void main(String[] args)
    {
  
        Map<String, Integer> hashMap = new HashMap<>();
  
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
          
        // copyMap method would copy the original 
        // hashMap to second_Map
        Map<String, Integer> second_map = copyMap(hashMap);
  
        System.out.println(second_map);
    }
}
Output
{A=1, B=2, C=3}

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :