This is a Java program that demonstrates how to create a shallow copy of a HashMap using the clone() method. Here's how it works:
import java.util.*; public class Shallow_Copy { public static void main(String args[]) { HashMap<Integer,String> hash_map = new HashMap<Integer,String>(); hash_map.put(1, "Pink"); hash_map.put(2, "Green"); hash_map.put(3, "Red"); hash_map.put(4, "White"); hash_map.put(5, "Blue"); hash_map.put(6, "Black"); hash_map.put(7, "Orange"); System.out.println("HashMap : " + hash_map); HashMap<Integer,String> new_hashmap = new HashMap<Integer,String>(); new_hashmap = (HashMap)hash_map.clone(); System.out.println("Cloned HashMap : " + new_hashmap); } }
HashMap : {1=Pink, 2=Green, 3=Red, 4=White, 5=Blue, 6=Black, 7=Orange} Cloned HashMap : {1=Pink, 2=Green, 3=Red, 4=White, 5=Blue, 6=Black, 7=Orange}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions