How to Copy One HashMap to Another HashMap in Java?
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:
- By normally iterating and putting it to another HashMap using put(k, v) method.
- Using putAll() method.
- Using copy constructor.
- Using assignment operator.
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
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<>();
for (Map.Entry<K, V> entry : original.entrySet()) {
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 );
Map<String, Integer> second_Map = copyMap(hashMap);
System.out.println(second_Map);
}
}
|
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
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<>();
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 );
Map<String, Integer> second_map = copyMap(hashMap);
System.out.println(second_map);
}
}
|
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 {
public static <K, V> Map<K, V> copyMap(Map<K, V> original)
{
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 );
Map<String, Integer> second_map = copyMap(hashMap);
System.out.println(second_map);
}
}
|
Method 4 : Using Assignment Operator
In java, if both the objects are same then we can easily copy or assign one object elements to other by simply using assignment operator “=”.
Java
import java.util.*;
class GFG {
public static void main (String[] args) {
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put( "A" , 1 );
hashMap.put( "B" , 2 );
hashMap.put( "C" , 3 );
Map<String, Integer> second_map = hashMap;
System.out.println(second_map);
}
}
|
Output :
{A=1, B=2, C=3}
Last Updated :
04 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...