how to pass a TreeMap as a parameter to some other collection and vice versa using collections in java
Previous | Home | Next |
In this example we shall see how to pass a TreeMap as parameter to some other collection which extends Map and vice-versa.
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 for adding the contents of a TreeMap to some other collection which extends the AbstractMap we have used the putAll(Map map) on the object of collection. The basic syntax of the putAll() method is,
void putAll(Map map)
This method copies all of the mappings from the specified map passed as an argument to the method to the current map.
In this example we have created a TreeMap tm and added some elements to it by using the put(Object key, Object value) method on it. Then we have displayed the contents of the TreeMap tm.
We have also created a Hashtable ht, and in the constructor of the Hashtable we have passes the TreeMap object tm as a parameter which is shown by the following statement in the example,Hashtable<Integer, String> ht=new Hashtable<Integer, String>(tm);
After initializing the Hashtable with all of the HashMap elements, we added some more elements to it by using the put(Object key, Object value) method on its object.Then we displayed the contents the Hashtable.
Then, we have used the addAll(Map map) on the object of the TreeMap and passed the Hashtable as the parameter, which results in the addition of the values of Hashtable to the TreeMap.
This is shown by the following statement in the example,
tm.putAll(ht);
Finally we have created the Set of the elements of the TreeMap and invoked iterator on that set and finally displayed the elements of the TreeMap.
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();
}
Hashtable<Integer, String> ht=new Hashtable<Integer, String>(tm); // creating a new Hashtable by passing the TreeMap collection
// as a argument for the Hashtable's constructor
ht.put(6, "e-learning"); // adding more elements to the Hashtable other than TreeMap's element
ht.put(7, "enhances");
ht.put(8, "knowledge");
System.out.println("The contents of the Hashtable are as follows: ");
set=ht.entrySet(); // creating a set of the contents of the Hashtable ht
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 Hashtable
String value=map.getValue(); // getting the values contained in the Hashtable
System.out.print("KEY := "+key); // printing the key
System.out.print(","+"VALUE := "+value); // printing the value
System.out.println();
}
tm.putAll(ht); // Now putting all the values of Hashtable back to the TreeMap tm
System.out.println("The contents of the TreeMap after adding the contents of Hashtable are: ");
set=tm.entrySet(); // creating a set of the contents of the Hashtable ht
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();
}
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(ClassCastException e)
{
e.printStackTrace();
}
}
}
The output of the above created 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 Hashtable are as follows:
KEY := 8,VALUE := knowledge
KEY := 7,VALUE := enhances
KEY := 6,VALUE := e-learning
KEY := 5,VALUE := s/w engineer
KEY := 4,VALUE := shashi
KEY := 3,VALUE := development
KEY := 2,VALUE := tutorials
KEY := 1,VALUE := r4r
The contents of the TreeMap after adding the contents of Hashtable are:
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 := enhances
KEY := 8,VALUE := knowledge
Previous | Home | Next |