It is quite simple to make a copy of the Map and assign it to other variable.
import java.util.*; public class MyApplication { public static void main(String[] args) { Mapx = new HashMap<>(); x.put("John", "Is a Name"); x.put("Java", "Is a Language"); Map y = new HashMap<>(x); System.out.println(y); } }
So, in the above code we have created a Map,
Mapx = 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.
Then we have created another Map y and passed the above Map x as Parameter.
Mapy = new HashMap<>(x);
And if we see the below output,
The new variable y is an exact copy of x.