Menu
Topics Index
...
`

WeakHashMapTest - Java Example Program


WeakHashMapTest
import java.util.*;

class WeakHashMapTest
{
        public static void main(String args[])
        {    
          Map weakmap=  (Map) new WeakHashMap();
          String one=new String("one");
          String two=new String("two");
          weakmap.put(one, "Lahari");
          weakmap.put(two, "Latha");
          System.gc();
          System.out.println("Before: "+weakmap.get("one")+" "+weakmap.get("two"));
          one=null;
          two=null;
           System.gc();
          System.out.println("After: "+weakmap.get("one")+" "+weakmap.get("two"));  
        }
}
OUTPUT

Before: Lahari Latha
After: null null

DESCRIPTION

In the above example program we have taken WeakHashMap,we will put entry for the weakmapobject and then garbage collected.In the WeakHashMap object entry will be there.Later we will make reference keys one,two as null,then garbage collected and again check the entry in WeakHashMap object there will no entry, it return null values.

© meritcampus 2019

All Rights Reserved.

Open In App