how to create a clone of TreeMap using the collections in java
Previous | Home | Next |
In this example we are creating the clone of a TreeMap.
For creating a TreeMap first of all we have to import the java.util package in which the java.util.TreeMap class is defined. In this example we have created a class named CollectionExample in which we have created a TreeMap and added some elements to it.
In this example we have used the clone() method on the object of the TreeMap i.e tm(in this example). The basic structure of the clone() method is,
Object clone()
This method returns a shallow copy of the TreeMap instance.
In this example we have created two TreeMap i.e tm and cloneMap. By using the put(Object key, Object value) method on the tm we have added some elements to the TreeMap. Then we have invoked the clone() method on the tm and stored the returned by this method in the cloneMap. This is done by the following statment in the example,cloneMap=(TreeMap<Integer, String>)tm.clone();
We also added some more elements to the cloneMap by using put(Object key, Object value) method on it. Finally 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, ClassCastException
{
try
{
TreeMap<Integer, String> tm=new TreeMap<Integer, String>();
tm.put(1, "r4r"); // adding elements to the treeSet
tm.put(2, "tutorials");
tm.put(3, "development");
tm.put(4, "shashi");
tm.put(5, "s/w engineer");
System.out.println("The contents of the TreeSet are as follows: ");
Set set=tm.entrySet(); // creating a set of the contents of the TreeSet
Iterator itr=set.iterator(); // Invoking the iterator on the set
while(itr.hasNext())
{
Map.Entry<Integer, String> map = (Map.Entry<Integer, String>)itr.next(); // using the Map.Entry interface
int key=map.getKey(); // getting the key contained in the treeSet
String value=map.getValue(); // getting the values contained in the treeSet
System.out.print("KEY := "+key); // printing the key
System.out.print(","+"VALUE := "+value); // printing the value
System.out.println();
}
TreeMap<Integer, String> cloneMap=new TreeMap<Integer, String>();
cloneMap=(TreeMap<Integer, String>)tm.clone();
cloneMap.put(6, "e-learning");
cloneMap.put(7, "is beneficial");
cloneMap.put(8, "for growth");
System.out.println("The contents of the cloned TreeMap cloneMap are as follows: ");
set=cloneMap.entrySet(); // creating a set of the contents of the cloned TreeSet cloneMap
itr=set.iterator(); // Invoking the iterator on the set
while(itr.hasNext())
{
Map.Entry<Integer, String> map = (Map.Entry<Integer, String>)itr.next(); // using the Map.Entry interface
int key=map.getKey(); // getting the key contained in the cloneMap
String value=map.getValue(); // getting the values contained in the cloneMap
System.out.print("KEY := "+key); // printing the key
System.out.print(","+"VALUE := "+value); // printing the value
System.out.println();
}
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(ClassCastException e)
{
e.printStackTrace();
}
}
}
The output of the above given example is as follows:
The contents of the TreeSet are as follows:
KEY := 1,VALUE := r4r
KEY := 2,VALUE := tutorials
KEY := 3,VALUE := development
KEY := 4,VALUE := shashi
KEY := 5,VALUE := s/w engineer
The contents of the cloned TreeMap cloneMap are as follows:
KEY := 1,VALUE := r4r
KEY := 2,VALUE := tutorials
KEY := 3,VALUE := development
KEY := 4,VALUE := shashi
KEY := 5,VALUE := s/w engineer
KEY := 6,VALUE := e-learning
KEY := 7,VALUE := is beneficial
KEY := 8,VALUE := for growth
Previous | Home | Next |