Given a HashMap, there are three ways one can copy the given HashMap to another:
Method 1: By normally iterating and putting it to another HashMap using put(k, v) Method.
// 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:
// 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 among the shortest and easiest ways to repeat one HashMap to a different .
We can use a replica constructor to repeat a map which may be a special constructor for creating a replacement object as a replica of an existing object.
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}
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.