import java.util.HashMap; public class HashMapDemo { public static void main(String[] args) { HashMapmap = new HashMap (); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); // creating a clone of the HashMap object HashMap mapClone = (HashMap ) map.clone(); // printing the values of original and cloned HashMaps System.out.println("Original Map: " + map); System.out.println("Clone Map: " + mapClone); } } Output: Original Map: {1=one, 2=two, 3=three} Clone Map: {1=one, 2=two, 3=three}
import java.util.HashMap; public class HashMapDemo { public static void main(String[] args) { HashMapPackage library: java.utilmap = new HashMap (); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); // creating a clone of the HashMap object HashMap mapClone = (HashMap ) map.clone(); // changing the value of a key-value pair in the cloned HashMap object mapClone.put(1, "four"); // printing the values of original and cloned HashMaps System.out.println("Original Map: " + map); System.out.println("Clone Map: " + mapClone); } } Output: Original Map: {1=one, 2=two, 3=three} Clone Map: {1=four, 2=two, 3=three}