Java Hashtable clone() Method
Description
The Java Hashtable clone() method is used to create a shallow copy of this hashtable.
Declaration
Following is the declaration for java.util.Hashtable.clone() method.
public Object clone()
Parameters
NA
Return Value
The method call returns a clone of the hashtable.
Exception
NA
Cloning a HashTable of Integer, Integer Pair Example
The following example shows the usage of Java Hashtable clone() method to get a shallow copy of a Hashtable. We've created two Hashtable objects of Integer,Integer pairs. Then few entries are added to one table, another table is populated using clone() method and then both hashtables are printed.
package com.tutorialspoint;
import java.util.Hashtable;
public class HashtableDemo {
public static void main(String args[]) {
// create two hash hashtables
Hashtable<Integer, Integer> hashtable1 = new Hashtable<>();
Hashtable<Integer, Integer> hashtable2 = new Hashtable<>();
// populate 1st table
hashtable1.put(1, 1);
hashtable1.put(2, 2);
hashtable1.put(3, 3);
// clone 1st table
hashtable2 = (Hashtable)hashtable1.clone();
System.out.println("1st Hashtable: " + hashtable1);
System.out.println("Cloned 2nd Hashtable: " + hashtable2);
}
}
Output
Let us compile and run the above program, this will produce the following result.
1st Hashtable: {3=3, 2=2, 1=1}
Cloned 2nd Hashtable: {3=3, 2=2, 1=1}
Cloning a HashTable of Integer, String Pair Example
The following example shows the usage of Java Hashtable clone() method to get a shallow copy of a Hashtable. We've created two Hashtable objects of Integer,String pairs. Then few entries are added to one table, another table is populated using clone() method and then both hashtables are printed.
package com.tutorialspoint;
import java.util.Hashtable;
public class HashtableDemo {
public static void main(String args[]) {
// create two hash hashtables
Hashtable<Integer, String> hashtable1 = new Hashtable<>();
Hashtable<Integer, String> hashtable2 = new Hashtable<>();
// populate 1st table
hashtable1.put(1, "A");
hashtable1.put(2, "B");
hashtable1.put(3, "C");
// clone 1st table
hashtable2 = (Hashtable)hashtable1.clone();
System.out.println("1st Hashtable: " + hashtable1);
System.out.println("Cloned 2nd Hashtable: " + hashtable2);
}
}
Output
Let us compile and run the above program, this will produce the following result.
1st Hashtable: {3=C, 2=B, 1=A}
Cloned 2nd Hashtable: {3=C, 2=B, 1=A}
Cloning a HashTable of Integer, Object Pair Example
The following example shows the usage of Java Hashtable clone() method to get a shallow copy of a Hashtable. We've created two Hashtable objects of Integer,Student pairs. Then few entries are added to one table, another table is populated using clone() method and then both hashtables are printed.
package com.tutorialspoint;
import java.util.Hashtable;
public class HashtableDemo {
public static void main(String args[]) {
// create two hash hashtables
Hashtable<Integer, Student> hashtable1 = new Hashtable<>();
Hashtable<Integer, Student> hashtable2 = new Hashtable<>();
// populate hash table
hashtable1.put(1, new Student(1, "Julie"));
hashtable1.put(2, new Student(2, "Robert"));
hashtable1.put(3, new Student(3, "Adam"));
// clone 1st table
hashtable2 = (Hashtable)hashtable1.clone();
System.out.println("1st Hashtable: " + hashtable1);
System.out.println("Cloned 2nd Hashtable: " + hashtable2);
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
Output
Let us compile and run the above program, this will produce the following result.
1st Hashtable: {3=[ 3, Adam ], 2=[ 2, Robert ], 1=[ 1, Julie ]}
Cloned 2nd Hashtable: {3=[ 3, Adam ], 2=[ 2, Robert ], 1=[ 1, Julie ]}