How Will You Create a Shallow Copy of a Map in Java ?

In Java, most implementations of Map interface provide a constructor to create copy of another map. But the copy method is not synchronized.

Therefore, when a thread is copying the map, another thread can modify it.

To prevent such a scenario, we should use Collections.synchronizedMap() method to first create a thread-safe map.

Another way of to create a shallow copy is by using clone() method. But it is not considered as a recommended approach.



You May Interest

Why Did Oracle Release a New Version of Java Like Java 8 ?

What is the Difference Between Abstract Class and Interface in Ja ...

How Will You Make an Object Immutable in Java ?

What is the Different Type of Tag Library Groups in JSTL ?

What are the Differences Between Comparable and Comparator in Jav ...