How to Clone a Map in Java collection
GIAN Tutorials
9:10 PM
How to Clone a Map in Java collection Following are the 5 different ways to Clone a Map in Java. Example: {1=this, 2=For, 3=this} Method 1: Naive method 1. Create an object for the class map. 2. Put the elements into the map using the put() method. 3. Again create another object for the class map. 4. Now finally iterate the map and call put method to clone the initial map. Below is the implementation of the above approach: // Program to clone a Map in Java // Naive Method import java.util.*; class JavacloneExample { public static void main(String[] args) { // Creating an object for class Map Map<Integer, String> hash_Map = new HashMap<Integer, String>(); // putting elements into the map hash_Map.put(1, "this"); hash_Map.put(2, "For"); hash_Map.put(3, "this"); // Creating a new object for // class Map to clone a map Map<Inte