![]() |
VOOZH | about |
The Hashtable.clone() is a built-in method in the Java Hashtable class. This method is used to create a shallow copy of the existing hashtable. A shallow copy means the keys and values are not cloned themselves, only the structure is duplicated. This method returns a cloned copy of the hashtable with the same key-value mappings.
In this article, we are going to learn about the Hashtable.clone() method in Java with syntax and examples.
hash_table.clone();
The clone() method is useful when we need a duplicate hashtable for reading or modifying without changing the original hashtable. It copies the structure of the table including the key-value mappings, but not the objects themselves. If we make any changes in the cloned object structure, it does not affect the original.
Example 1: In this example, we are going to create a Hashtable with Integer keys and String values and then we will use the clone() method to clone it.
The Hashtable is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
The cloned table: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Example 2: In this example, we are going to create a Hashtable with String keys and Integer values and then we will use the clone() method to clone it.
The Hashtable is: {You=30, Welcomes=25, 4=15, Geeks=20}
The cloned table: {You=30, Welcomes=25, 4=15, Geeks=20}
Note: The same operation can be performed with any type of variation and combination of different data types.