Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to pass the WeakHashMap as an argument to some other collection and vice-versa using collections in java

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