how to create a clone of LinkedHashMap using collections in java
Previous | Home | Next |
In this example we shall create a clone of the LinkedHashMap.
For creating a LinkedHashMap first of all we have to import the java.util package in which the java.util.LinkedHashMap class is contained. In this example we have created a class named CollectionExample in which we have created a LinkedHashMap and added some elements to it
In the following example we have used the clone() method for creating the clone the LinkedHashMap. The general syntax of the clone() method is,
Object clone()
This method returns a shallow copy of the LinkedHashMap instance.
In this example we have created an object of the LinkedHashMap lhm and added some elements to the LinkedHashMap by using put(Object key, Object value) method on its object. Then we have displayed the contents of the LinkedHashMap.
We have created another LinkedHashMap named cloneMap and then stored the values returned by invoking the clone() method on the object of the LinkedHashMap lhm in the cloneMap.
We have created a Set of the values of the cloneMap and then invoked the iterator on the object of the Set set and by using the Map.Entry Interface we have displayed the contents of the cloneMap.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
LinkedHashMap<Integer, Integer> lhm=new LinkedHashMap<Integer, Integer>(); // creating a new LinkedHashMap
lhm.put(1, new Integer(7)); // adding elements to this LinkedHashMap
lhm.put(2, new Integer(5));
lhm.put(3, new Integer(8));
lhm.put(4, new Integer(2));
lhm.put(5, new Integer(9));
lhm.put(6, new Integer(52));
System.out.println("The contents of the LinkedHashMap are as follows: ");
Set set=lhm.entrySet(); // creating a set of the contents of the LinkedHashMap
Iterator itr=set.iterator(); // invoking the iterator on the set created
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry interface
Integer key = map.getKey(); // getting the keys
Integer value = map.getValue(); // getting the values
System.out.print("KEY: "+key); // printing the keys
System.out.print(" "+"VALUE: "+value); // printing the values
System.out.println();
}
LinkedHashMap<Integer, Integer> cloneMap=new LinkedHashMap<Integer, Integer>();
cloneMap=(LinkedHashMap<Integer, Integer>)lhm.clone();
System.out.println("The contents of the cloned LinkedHashMap are as follows: ");
set=lhm.entrySet(); // creating a set of the contents of the cloned LinkedHashMap
itr=set.iterator(); // invoking the iterator on the set created
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry interface
Integer key = map.getKey(); // getting the keys of the cloned LinkedHashMap
Integer value = map.getValue(); // getting the values of the cloned LinkedHashMap
System.out.print("KEY: "+key); // printing the keys
System.out.print(" "+"VALUE: "+value); // printing the values
System.out.println();
}
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
The output of the above given example is the following one :
The contents of the LinkedHashMap are as follows:
KEY: 1 VALUE: 7
KEY: 2 VALUE: 5
KEY: 3 VALUE: 8
KEY: 4 VALUE: 2
KEY: 5 VALUE: 9
KEY: 6 VALUE: 52
The contents of the cloned LinkedHashMap are as follows:
KEY: 1 VALUE: 7
KEY: 2 VALUE: 5
KEY: 3 VALUE: 8
KEY: 4 VALUE: 2
KEY: 5 VALUE: 9
KEY: 6 VALUE: 52
Previous | Home | Next |