how to pass the WeakHashMap as an argument to some other collection and vice-versa using collections in java
Previous | Home | Next |
In this example we are going to pass the WeakHashMap as an argument to some other collection which extends the AbstractMap and vice-versa.
For creating a WeakHashMap first of all we have to import the java.util package in which the java.util.WeakHashMap class is contained. In this example we have created a class named the CollectionExample, in which we have created the WeakHashMap and added some elements to it.
In this example we have created a method named AddCollection(WeakHashMap whm, Hashtable ht) which takes the WeakHashMap object and Hashtable object as a parameter. This method will add the contents of the Hashtable to the WeakHashMap.
Inside this method we have used the putAll(Map map) method for adding the elements a collection which extends AbstractMap. In this example we have added the contents of the Hashtable to the WeakHashMap, which is done by the following statement of the example,whm.putAll(ht);
In the following example we have created a class name CollectionExample, in which we have created a WeakHashMap which contains the integral keys and integral values in it. The WeakHashMap object is created by using the default WeakHashMap constructor which is done by following statement,WeakHashMap<Integer, Integer> whm = new WeakHashMap<Integer, Integer>();
The above statement constructs a new, empty WeakHashMap with the default initial
capacity (16) and the default load factor (0.75).
We have also created a Hashtable object whose object we are going to add to the WeakHashMap. Then we have created an object of the CollectionExample class an call the AddCollection() method on it. This method will add all the elements of the Hashtable to the WeakHashMap.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException, ClassCastException
{
try
{
WeakHashMap<Integer, Integer> whm = new WeakHashMap<Integer, Integer>(); // creating a WeakHashMap
whm.put(1, 1); // adding elements to the WeakHashMap
whm.put(2, 2);
whm.put(3, 3);
whm.put(4, 4);
whm.put(5, 5);
System.out.println("The key value pair of the WeakHashMap is given as: ");
Set<Map.Entry<Integer, Integer>> set=whm.entrySet(); // creating a set of the elements of the WeakHashMap
Iterator<Map.Entry<Integer, Integer>> 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 Map.Entry interface
System.out.print("Key:= "+map.getKey()); // retrieving the keys
System.out.print(" Value:= "+map.getValue()); // retrieving the values
System.out.println();
}
Hashtable<Integer, Integer> ht=null;
CollectionExample ce=new CollectionExample(); // creating an object of the class CollectionExample
ce.AddCollection(whm, ht); // calling the AddCollection method on the object created
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(ClassCastException e)
{
e.printStackTrace();
}
}
void AddCollection(WeakHashMap<Integer, Integer> whm, Hashtable<Integer, Integer> ht) //creating a method for adding some other collection to the WeakHashMap
{
ht = new Hashtable<Integer, Integer>(whm); // here we have created a Hashtable by passing the WeakHashSet as an argument to its constructor
ht.put(6, 6); // adding elements to the Hashtable ht
ht.put(7, 7);
ht.put(8, 8);
ht.put(1, 72);
System.out.println("The contents of the Hashtable after adding contents of the WeakHashMap are: ");
Set<Map.Entry<Integer, Integer>> set=ht.entrySet(); // creating a set of values contained in Hashtable ht
Iterator<Map.Entry<Integer, Integer>> itr=set.iterator(); // Invoking iterator on the set
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using Map.Entry Interface for separating keys and values
System.out.print("Key:= "+map.getKey()); // displaying the keys of the Hashtable
System.out.print(" Value:= "+map.getValue()); // displaying the values of the Hashtable
System.out.println();
}
whm.putAll(ht); // here we are putting all the values of the Hashtable to the WeakHashMap
whm.put(9, 9); // adding values to WeakHashMap
whm.put(11, 11);
System.out.println("After adding the Hashtable to WeakHashMap and some additional elements, the contents of WeakHashMap are: ");
set=whm.entrySet(); // creating set of the values of the WeakHashMap
itr=set.iterator(); // Invoking iterator on the set
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry Interface
System.out.print("Key:= "+map.getKey()); // retrieving the keys
System.out.print(" Value:= "+map.getValue()); // retrieving the values
System.out.println();
}
}
}
The output of the above given example will be as follows:
The key value pair of the WeakHashMap is given as:
Key:= 5 Value:= 5
Key:= 4 Value:= 4
Key:= 3 Value:= 3
Key:= 2 Value:= 2
Key:= 1 Value:= 1
The contents of the Hashtable after adding contents of the WeakHashMap are:
Key:= 8 Value:= 8
Key:= 7 Value:= 7
Key:= 6 Value:= 6
Key:= 5 Value:= 5
Key:= 4 Value:= 4
Key:= 3 Value:= 3
Key:= 2 Value:= 2
Key:= 1 Value:= 72
After adding the Hashtable to WeakHashMap and some additional elements, the contents of WeakHashMap are:
Key:= 11 Value:= 11
Key:= 9 Value:= 9
Key:= 8 Value:= 8
Key:= 7 Value:= 7
Key:= 6 Value:= 6
Key:= 5 Value:= 5
Key:= 4 Value:= 4
Key:= 3 Value:= 3
Key:= 2 Value:= 2
Key:= 1 Value:= 72
Previous | Home | Next |