how to remove and add elements to a WeakHashMap using collections in java
Previous | Home | Next |
In this example we are going to remove and add elements to a WeakHashMap.
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 following methods,
void AddCollection(WeakHashMap obj, Hashtable obj)
This method add the contents of the Hashtable to the WeakHashMap by using the putAll(Map map) on the object of the WeakHashMap.
void modify(WeakHashMap obj, Hashtable obj)
This method adds and removes various elements from the WeakHashMap and Hashtable.
In this example we have created a WeakHashMap by using its default constructor. Then we have added some elements to the WeakHashMap by using the put(Object key, Object value) method on its object i.e. whm(in this example). We have also created a Hashtable and initialized it with the contents of the WeakHashMap and then we have called the AddCollection() in the main().
We have also created a method named modify(WeakHashMap obj, Hashtable obj) which adds and removes various elements from the Hashtable and WeakHashMap.
Finally we have shown the contents of the Hashtable and WeakHashMap by using the ShowCollection(WeakHashMap obj, Hashtable obj) which gives the current status of the WeakHashMap and Hashtable.
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=new Hashtable<Integer, Integer>(whm); // creating an object of Hashtable by passing the WeakHashmap object to its constructor
AddCollection obj=new AddCollection();
obj.AddCollection1(whm, ht); // calling the AddCollection1 method on AddCollection object
obj.ShowCollection(whm, ht); // calling the ShowCollection method
obj.modify(whm, ht); // calling the modify method
System.out.println("After modifying the WeakHashMap contains the elements: "+whm); // Now displaying the status of WeakHashMap after modifying it
System.out.println("After modifying the Hashtable contains the elements: "+ht); // displaying the status of WeakHashMap after modifying it
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(ClassCastException e)
{
e.printStackTrace();
}
}
}
class AddCollection // creating a new class named AddCollection
{
public void AddCollection1(WeakHashMap<Integer, Integer> whm, Hashtable<Integer, Integer> ht) //creating a method for adding some other collection to the WeakHashMap
{
System.out.println("Initially the elements of Hashtable upon creation are: "+ht);
ht.put(6, 6); // adding elements to the Hashtable ht
ht.put(7, 7);
ht.put(8, 8);
ht.put(17, 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();
}
}
public void ShowCollection(WeakHashMap<Integer, Integer> whm, Hashtable<Integer, Integer> ht) // creating a method for showing the collection elements
{
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<Map.Entry<Integer, Integer>> set=whm.entrySet(); // creating set of the values of the WeakHashMap
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 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();
}
}
public void modify(WeakHashMap<Integer, Integer> whm, Hashtable<Integer, Integer> ht) // a method for removing or adding various elements
{
System.out.println("The current status of Hashtable is: "+ht); // displaying the current status of Hashtable
System.out.println("The current status of WeakHashMap is: "+whm); // displaying the current status of WeakHashMap
ht.remove(4); // removing the element with key 4 from Hashtable
ht.remove(3); // removing the element with key 3 from Hashtable
System.out.println("The retrieved element is:"+ht.get(1)); // getting the element with key 1 from Hashtable
whm.remove(11); // removing element with key 11 from WeakHashMap
whm.remove(9); // removing element with key 9 from WeakHashMap
ht.put(14, 18); // adding element to the Hashtable
ht.put(15, 19);
whm.put(20, 70); // adding elements to the WeakHashMap
whm.put(21, 20);
}
}
The output of the above given example is 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
Initially the elements of Hashtable upon creation are: {5=5, 4=4, 3=3, 2=2, 1=1}
The contents of the Hashtable after adding contents of the WeakHashMap are:
Key:= 17 Value:= 72
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:= 1
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:= 1
Key:= 17 Value:= 72
The current status of Hashtable is: {17=72, 8=8, 7=7, 6=6, 5=5, 4=4, 3=3, 2=2, 1=1}
The current status of WeakHashMap is: {11=11, 9=9, 8=8, 7=7, 6=6, 5=5, 4=4, 3=3, 2=2, 1=1, 17=72}
The retrieved element is:1
After modifying the WeakHashMap contains the elements: {8=8, 7=7, 6=6, 20=70, 5=5, 21=20, 4=4, 3=3, 2=2, 1=1, 17=72}
After modifying the Hashtable contains the elements: {17=72, 15=19, 14=18, 8=8, 7=7, 6=6, 5=5, 2=2, 1=1}
Previous | Home | Next |