how to create a clone of a Hashtable using collections in java
Previous | Home | Next |
In this example we will create the clone of a given Hashtable.
For creating a Hashtable first of all we have to import the java.util package in which the java.util.Hashtable class is defined. In this example we have created a class named CollectionExample in which we have created a Hashtable and added some elements to it.
For creating the clone of the Hashtable we have used the clone() method on the object of the Hashtable. The syntax of the clone() method is as follows:
Object clone()
This method creates a shallow copy the current Hashtable.
In this example we have created a Hashtable object ht and added the elements to the Hashtable by using put(Object key, Object value) method on the ht. We have also created another Hashtable which is empty, on the ht we have applied the clone() method which returns the clone of the elements of the ht and are stored in the empty Hashtable ht2 as,ht2=(Hashtable<Integer, String>)ht.clone();
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String[] args) throws NullPointerException
{
// TODO Auto-generated method stub
try
{
Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); // created a Hashtable named ht in which key is numeric and value is String types
ht.put(1, "r4r"); // adding the values to the hashtable using key-value pairs
ht.put(2, "development");
ht.put(3, "tutorials");
ht.put(4, "shashi");
ht.put(5, "software developer");
System.out.println("The size of the Hashtable ht is: "+ht.size()); // displaying the size of the Hashtable
System.out.println("The elements according to key are: ");
for(int i=1;i<=ht.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ ht.get(i)); // displaying the key-value pair contained in the Hashtable
}
Hashtable<Integer, String> ht2=new Hashtable<Integer, String>(); // created a second Hashtable named ht2 which is empty
ht2=(Hashtable<Integer, String>)ht.clone(); // here we have created a clone of ht to ht2
System.out.println("The size of the cloned Hashtable ht2 is: "+ht2.size()); // here we are displaying the size of Hashtable ht2
System.out.println("The key-value pair in the cloned Hashtable ht2 are as follows: ");
for(int i=1;i<=ht2.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ ht2.get(i)); // displaying the key-value pair contained in cloned Hashtable ht2
}
}
catch(NullPointerException e) // handling the NullPointer Exception which is thrown
{
e.printStackTrace();
}
}
}
The size of the Hashtable ht is: 5
The elements according to key are:
key = 1,Value = r4r
key = 2,Value = development
key = 3,Value = tutorials
key = 4,Value = shashi
key = 5,Value = software developer
The size of the cloned Hashtable ht2 is: 5
The key-value pair in the cloned Hashtable ht2 are as follows:
key = 1,Value = r4r
key = 2,Value = development
key = 3,Value = tutorials
key = 4,Value = shashi
key = 5,Value = software developer
Previous | Home | Next |