Java Utility Library

Java Hashtable - clone() Method



The java.util.Hashtable.clone() method returns a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned.

Syntax

public Object clone()

Parameters

No parameter is required.

Return Value

Returns a shallow copy of this hashtable.

Exception

NA.

Example:

In the example below, the java.util.Hashtable.clone() method returns a shallow copy of the given hashtable.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating hashtable
    Hashtable<Integer, String> Htable1 = new Hashtable<Integer, String>();
    
    //populating Htable1
    Htable1.put(101, "John");
    Htable1.put(102, "Marry");
    Htable1.put(103, "Kim");
    Htable1.put(104, "Jo");

    //create a copy of Htable1 into Htable2
    Object Htable2 = Htable1.clone();

    //print the content of Htable2
    System.out.print("Htable2 contains: "+ Htable2);  
  }
}

The output of the above code will be:

Htable2 contains: {104=Jo, 103=Kim, 102=Marry, 101=John}

❮ Java.util - Hashtable