how to create a clone of a HashMap using the collections in java
Previous | Home | Next |
In this example we are creating the clone of a HashMap using collections.
For creating a HashMap first of all we have to import the java.util package in which the java.util.HashMap class is contained which is used for creating the HashMap. In this example we have created a class named CollectionExample in which we have created HashMap and added some elements to it.
In this example we have used the clone() method on the object of the HashMap for creating the clone of the HashMap. The syntax of the clone() in general is ,This method returns a shallow copy of the HashMap instance.
Object clone()
In this example we have created a HashMap which contains String keys and values of integral types. We have added some elements this HashMap object hm by using the put(Object key, Object value) method it.
We have also created Hashtable and added all the elements of it to the HashMap by using the putAll(Map map) method.
We have created another HashMap named cloneMap of same type as HashMap hm. The values returned by invoking the clone() method on the object of the HashMap hm are stored in this cloneMap. Finally we have displayed the values this cloneMap via using Iterator interface.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
HashMap<String, Integer> hm = new HashMap<String, Integer>(); // creating a HashMap having keys in string format and values in Integer
hm.put("one-> ", 1); // adding values to the HashMap
hm.put("two-> ", 2);
hm.put("three-> ", 3);
hm.put("four-> ", 4);
hm.put("five-> ", 5);
System.out.println("The key contained in the HashMap are as follows: ");
Iterator<String> itr=hm.keySet().iterator(); // Invoking iterator on the key set of the hashmap
Iterator<Integer> itr1=hm.values().iterator(); // Invoking iterator on the value set of the hashmap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // displaying the keys of the hashmap
System.out.print(itr1.next()); // displaying the values of the hashmap
System.out.println();
}
Hashtable<String, Integer> htbl=new Hashtable<String, Integer>(); // creating a Hashtable named htbl
htbl.put("five->", 5); // adding values to the hashtable
htbl.put("six-> ", 6);
htbl.put("seven-> ", 7);
htbl.put("eight-> ", 8);
htbl.put("nine-> ", 9);
System.out.println("The contents of the created Hashtable htbl are: ");
itr=htbl.keySet().iterator(); // Invoking iterator on the keyset of the hashtable
itr1=htbl.values().iterator(); // Invoking iterator on the values set of the hashtable
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // displaying the keys of the hashtable
System.out.print(itr1.next()); // displaying the values of the hashtable
System.out.println();
}
hm.putAll(htbl); // adding the Hashtable collection to the HashMap
System.out.println("Now the contents of the HashMap after adding all the contents of the Hashtable are: ");
itr=hm.keySet().iterator(); // Invoking the iterator on the keyset of the resulted HashMap
itr1=hm.values().iterator(); // invoking the iterator on the value set of the resulted HashMap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // diplaying the keys of the resulted HashMap
System.out.print(itr1.next()); // displaying the values of the resulted HashMap
System.out.println();
}
HashMap<String, Integer> cloneMap=new HashMap<String, Integer>(); // creating an empty hashMap named cloneMap
cloneMap=(HashMap<String, Integer>)hm.clone(); // creating a clone of the HashMap hm and storing it to cloneMap
System.out.println("The contents of the cloned HashMap are as follows: ");
itr=cloneMap.keySet().iterator(); // Invoking the iterator on the keyset of the cloneMap HashMap
itr1=cloneMap.values().iterator(); // invoking the iterator on the value set of the cloneMap HashMap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // diplaying the keys of the cloneMap HashMap
System.out.print(itr1.next()); // displaying the values of the cloneMap HashMap
System.out.println();
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}
}
The output of the above written example is as follows:
The key contained in the HashMap are as follows:
two-> 2
one-> 1
five-> 5
three-> 3
four-> 4
The contents of the created Hashtable htbl are:
nine-> 9
six-> 6
eight-> 8
seven-> 7
five->5
Now the contents of the HashMap after adding all the contents of the Hashtable are:
eight-> 8
six-> 6
nine-> 9
two-> 2
one-> 1
seven-> 7
five-> 5
three-> 3
five->5
four-> 4
The contents of the cloned HashMap are as follows:
eight-> 8
two-> 2
nine-> 9
six-> 6
one-> 1
seven-> 7
three-> 3
five-> 5
five->5
four-> 4
Previous | Home | Next |