VOOZH about

URL: https://www.geeksforgeeks.org/java/java-hashtable-clone-method/

⇱ Java Hashtable clone() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Hashtable clone() Method

Last Updated : 11 Jul, 2025

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.

Syntax of Hashtable clone() Method

hash_table.clone();

  • Parameters: The method does not take any parameters. 
  • Return Value: The method just returns a shallow copy of the Hashtable.

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.

Examples of Java Hashtable clone() Method

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.


Output
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.


Output
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.

Comment