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 create a WeakHashMap using collections in java

how to create a WeakHashMap using collections in java

Previous Home Next

 

In this example we are going to create 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 a WeakHashMap by using the default constructor of the WeakHashMap class. This is done by the 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 used the put(Object key, Object value) method for adding the elements to the WeakHashMap. The syntax of put() method is,
put(Object key, Object value)
This method adds the specified value with the specified key in this WeakHashMap.

In this example we have created a class named CollectionExample in which we have created a WeakHashMap whose keys are of Integer type and values are also of Integer type, and by using the put(Object key, Object value) method on its object we have added some elements to it, which is done by the following line in the example,
whm.put(1, 1);
where whm is the object of the WeakHashMap which places the integer value 1 corresponding to the key 1.
Finally we have displayed the contents of the WeakHashMap by invoking the iterator on it.

 
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>();
whm.put(1, 1);
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();
Iterator<Map.Entry<Integer, Integer>> itr=set.iterator();
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next();
System.out.print("Key:= "+map.getKey());
System.out.print(" Value:= "+map.getValue());
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 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

Previous Home Next