How to clone a map in Java

Hello programmers, in this tutorial you will how to clone a Map in Java. Making a clone of a map basically in programming means that both the entries of map 1 and map 2 will be the same but stored in different locations in the memory space.

In this tutorial you will learn two ways of cloning a Map in Java:

1)Using the constructor of HashMap.

2)Using putAll() method.

Using a constructor of HashMap

  • To create a clone of the original Map here, in this method you will be using a HashMap constructor.
HashMap(Map<? extends K,? extends V> m)
  • First, you will create a HashMap of key type integer and value type as a string.
  • Then you need to store some entries into it. So you will use the “.put” method which will help store in the key-value pair in the HashMap.
  • After that, you need to create another HashMap of the same key as integer and value as string pair. And in this, while creating the new HashMap you need to pass the 1st HashMap entries in the arguments of this HashMap-2 constructor.
  • Then what will happen is as you have passed the HashMap-1 entries in the constructor of HashMap-2. The HashMap-2 will also now get the same entries of HashMap-1 and will store them in itself without changing anything in HashMap-1.
  • Now, both HashMap-1 and HashMap-2 will have the same entries in it, but both are allocated different memory spaces.

Java Code To Clone A Map(Using constructor)

import java.util.*; 
  
public class CloneMap { 
  
    public static void main(String[] args) 
    { 
        
        Map<Integer, String> hm = new HashMap<Integer, String>(); 
    
       hm.put(1, "Red"); 
       hm.put(2, "Green"); 
       hm.put(3, "Yellow"); 
       hm.put(4, "Blue");
       hm.put(5, "Orange");
      
       System.out.println("First HashMap Entries: " + hm); 
      
        Map<Integer, String> new_Hashmap = new HashMap<Integer, String>(); 
  
        new_Hashmap = new HashMap<>(hm); 
        System.out.println("\n Original HashMap entries after copying to 2nd HashMap \n");
        System.out.println("Second HashMap Entries: " + new_Hashmap); 
    } 
}

OUTPUT:

First HashMap Entries: {1=Red, 2=Green, 3=Yellow,  4=Blue, 5=Orange}

 Original HashMap entries after copying to 2nd HashMap

Second HashMap Entries: {1=Red, 2=Green, 3=Yellow, 4 =Blue, 5=Orange}

Using putAll() method

  • To create a clone of the original Map here, in this method you will be using the putAll() method.
  • This putAll() method is basically used to copy all the entries from HashMap-1 to HashMap-2. It selects all the key-value entry pairs from HashMap-1 and then stores it into HashMap-2.
  • So to use this method First, you need to create a HashMap with key as integer and value as string pair.
  • Then you need to store some entries into it. So you will use the “.put” method which will help store in the key-value pair in the HashMap.
  • After that, you need to create another HashMap of the same key as integer and value as string pair.
  • Then you need to write the new HashMap-2 object and with using a “.” operator write this putAll() method with it and pass the HashMap-1 object into it. By doing so you are passing all the entries or mappings of HashMap-1 into HashMap-2.
  • In conclusion, HashMap-1 entries are now copied into HashMap-2 and Both The HashMaps now contain the same entries or mappings of key-value pairs.

Java Code To Clone A Map(Using putAll() method)

import java.util.*; 
  
public class CloneMap2{ 
  
    public static void main(String[] args) 
    { 
        
        Map<Integer, String> hm = new HashMap<Integer, String>(); 
   
       hm.put(1, "Red"); 
       hm.put(2, "Green"); 
       hm.put(3, "Yellow"); 
       hm.put(4, "Blue");
       hm.put(5, "Orange");
      
       System.out.println("First HashMap Entries: " + hm); 
      
        Map<Integer, String> new_HashMap = new HashMap<Integer, String>(); 
  
        new_HashMap.putAll(hm); 
        System.out.println("\n Original HashMap entries after copying to 2nd HashMap \n");
        System.out.println("Second HashMap Entries: " + new_HashMap); 
    } 
}

OUTPUT:

First HashMap Entries: {1=Red, 2=Green, 3=Yellow,  4=Blue, 5=Orange}

 Original HashMap entries after copying to 2nd HashMap

Second HashMap Entries: {1=Red, 2=Green, 3=Yellow, 4 =Blue, 5=Orange}

 

Also Read: Solve Coin Change problem in Java

Java Program to merge two files in a third file

Leave a Reply

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