WeakHashMap Class

WeakHashMap Class

In this tutorial, we are going to discuss about WeakHashMap Class in Java. The WeakHashMap class in Java is an implementation of the Map interface that uses weak references for its keys.

Unlike other map implementations such as HashMap, WeakHashMap does not prevent the garbage collector from collecting its keys if they are not referenced elsewhere in the application. This makes it useful for scenarios where temporary associations between objects are needed, and it’s acceptable for those associations to be automatically removed when the keys are no longer strongly reachable.

WeakHashMap Class

It is exactly same as HashMap except the following differences

  • In the case of normal HashMap, an object is not eligible for GC even though it doesn’t have any references if it is associated with HashMap. That is HashMap dominates garbage collector.
  • But in the case of WeakHashMap if an object does not have any references then it’s always eligible for GC even though it is associated with WeakHashMap that is garbage collector dominates WeakHashMap.
package com.ashok.collections;

import java.util.HashMap;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyHashMap {
   public static void main(String[] args) throws Exception {
      HashMap map = new HashMap();
      MyKey key = new MyKey();
      map.put(key, "Ashok");
      System.out.println(map);
      key = null;
      System.gc();
      Thread.sleep(2000);
      System.out.println(map);
   }
}

class MyKey {
   public String toString() {
      return "Key is ";
   }

   public void finalize() {
      System.out.println("finalize() method called");
   }
} 

Output

{Key is =Ashok}
{Key is =Ashok}
package com.ashok.collections;

import java.util.WeakHashMap;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyWeakHashMap {
   public static void main(String[] args) throws Exception {
      WeakHashMap map = new WeakHashMap();
      MyKey key = new MyKey();
      map.put(key, "Ashok");
      System.out.println(map);
      key = null;
      System.gc();
      Thread.sleep(2000);
      System.out.println(map);
   }
}

class MyKey {
   public String toString() {
      return "Key is ";
   }

   public void finalize() {
      System.out.println("finalize() method called");
   }
}

Output

{Key is :=Ashok}
finalize() method called
{}
Key Features of WeakHashMap
  1. Weak References: WeakHashMap class uses weak references for its keys. If a key is not strongly referenced elsewhere in the application, it becomes eligible for garbage collection, and the corresponding key-value mapping is automatically removed from the map.
  2. Automatic Cleanup: Weak references allow WeakHashMap class to automatically clean up unused mappings when keys are no longer strongly reachable. This can be useful for managing resource cleanup and avoiding memory leaks.
  3. Not Suitable for Strong References: Since WeakHashMap uses weak references, it is not suitable for scenarios where strong references to keys are required to prevent their premature garbage collection.
  4. Performance Considerations: The performance of WeakHashMap may be impacted by the overhead of weak reference management and the periodic cleanup of unreachable keys.
  5. Not Synchronized: Like other map implementations, WeakHashMap is not synchronized. If thread safety is required, synchronization must be handled externally.
  6. Ordering: WeakHashMap does not guarantee any specific order of its elements, similar to HashMap.

That’s all about the WeakHashMap class in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

WeakHashMap Class
Scroll to top