HashtableoriginalTable = new Hashtable (); originalTable.put(1, "Java"); originalTable.put(2, "Python"); originalTable.put(3, "C++"); // creating a shallow copy of the original hashtable Hashtable copiedTable = (Hashtable ) originalTable.clone(); // modifying the value in the original hashtable originalTable.put(2, "JavaScript"); System.out.println("Original hashtable: " + originalTable); // prints {1=Java, 2=JavaScript, 3=C++} System.out.println("Copied hashtable: " + copiedTable); // prints {1=Java, 2=Python, 3=C++}
import java.util.*; class Main { public static void main(String[] args) { HashtableIn this example, two Hashtable instances named table1 and table2 are created and some key-value pairs are added to them. The putAll() method is used to merge the two hashtables. Then a copy of table1 is created using the clone() method and stored in copy. The output shows that the merged hashtable is copied correctly to the new hashtable instance. The java.util package library is used for the Hashtable class and its methods.table1 = new Hashtable (); table1.put("Java", 1); table1.put("Python", 2); // creating another hashtable and adding data to it Hashtable table2 = new Hashtable (); table2.put("C++", 3); table2.put("JavaScript", 4); // merging the two hashtables table1.putAll(table2); // creating a new hashtable as a copy of table1 Hashtable copy = (Hashtable ) table1.clone(); System.out.println("Original hashtable: " + table1); // prints {Java=1, Python=2, C++=3, JavaScript=4} System.out.println("Copied hashtable: " + copy); // prints {Java=1, Python=2, C++=3, JavaScript=4} } }