Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Copy from Map


It is quite simple to make a copy of the Map and assign it to other variable.


Example :



import java.util.*;

public class MyApplication {
    public static void main(String[] args) {

        Map x = new HashMap<>();

        x.put("John", "Is a Name");
        x.put("Java", "Is a Language");

        Map y = new HashMap<>(x);

        System.out.println(y);
    }
}


Output :



  {John=Is a Name, Java=Is a Language}

So, in the above code we have created a Map,


Map x = new HashMap<>();

And inserted the entries as Key and Value pairs using put() method.


x.put("John", "Is a Name");
x.put("Java", "Is a Language");

Below is how they are placed in the Map.

Spring_Boot

Then we have created another Map y and passed the above Map x as Parameter.


Map y = new HashMap<>(x);
Spring_Boot


And if we see the below output,

Output :



  {John=Is a Name, Java=Is a Language}

The new variable y is an exact copy of x.