The HashTable.clone()
method is present in the HashTable
class inside the java.util
package. It is used to return the shallow copy of the given HashTable
.
HashTable.clone()
does not take any parameters.
HashTable.clone()
returns the copy of HashTable
.
Let’s go over this with the help of an example.
Suppose we have a HashTable1
= {1 = Let's, 5 = see, 2 = Hashtable.clone(), 27 = method}
Upon using the HashTable.clone()
method, we store the result in the object clone of HashTable1
, and it returns the copy of HashTable1
and stores it in clone.
Let’s look at the below code snippet to understand it better.
import java.util.*;class Main{public static void main(String[] args){Hashtable<Integer, String> h1 = new Hashtable<Integer, String>();h1.put(1, "Let's");h1.put(5, "see");h1.put(2, "Hashtable.clone()");h1.put(27, "method");h1.put(9, "in java.");System.out.println("The Hashtable is: " + h1);System.out.println("The clone of specified HashTable is: " + h1.clone());}}
Main
class.main
function.HashTable
consisting of Integer
type keys and string
type values.Hashtable
by using the Hashtable.put()
method.Hashtable
.HashTable.clone()
method to create the clone of HashTable
and display the clone HashTable
with a message.In this way, we can use the HashTable.clone()
method to return the shallow copy of the given HashTable
.