Open In App

Java Hashtable clone() Method

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Java
// Java program to illustrate the clone() method
import java.util.*;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Create an empty Hashtable
        Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
        
        // Put values into the table
        ht.put(10, "Geeks");
        ht.put(15, "4");
        ht.put(20, "Geeks");
        ht.put(25, "Welcomes");
        ht.put(30, "You");

        System.out.println("The Hashtable is: " + ht);

        System.out.println("The cloned table: "
                           + ht.clone());
    }
}

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.

Java
// Java program to demonstrate the clone() method
import java.util.*;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Create an empty Hashtable
        Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
        
        // Put values into the table
        ht.put("Geeks", 10);
        ht.put("4", 15);
        ht.put("Geeks", 20); 
        ht.put("Welcomes", 25);
        ht.put("You", 30);

        System.out.println("The Hashtable is: " + ht);


        System.out.println("The cloned table: "
                           + ht.clone());
    }
}

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.


Next Article

Similar Reads