Java WeakHashMap

What is Java WeakHashMap? 

Java WeakHashMap is a class that implements the Map interface and provides a way to store key-value pairs in which the keys are weakly referenced.

This means that if the key has no strong references, it will be automatically removed from the map by the garbage collector.

To understand the Java WeakHashMap , Read the Complete Article.

Differences Between HashMap and WeakHashMap

  • Here are the main differences between HashMap and WeakHashMap in Java:
    • Performance: HashMap is generally faster than WeakHashMap because it does not need to manage weak references.

    • Thread safety: Both HashMap and WeakHashMap are not thread-safe by default, and may require external synchronization or the use of ConcurrentHashMap for thread safety.

    • Garbage Collection: HashMap does not facilitate garbage collection for its keys, whereas WeakHashMap allows the garbage collector to remove entries whose keys are no longer strongly referenced.
  • The choice between HashMap and WeakHashMap depends on the specific requirements of your program, such as the need for garbage collection, memory usage, and performance.

Let’s look at the Java WeakHashMap to perform certain operations.

Example 1:Java program to make an implementation on weak HashMap and HashMap

Run
import java.util.HashMap;
import java.util.WeakHashMap;

public class Main
{
  public static void main (String[]args)
  {
    // create a HashMap and add an entry
    HashMap < String, Integer > hashMap = new HashMap <> ();
    String key = "key";
    Integer value = 1;
      hashMap.put (key, value);

    // create a WeakHashMap and add an entry
      WeakHashMap < String, Integer > weakHashMap = new WeakHashMap <> ();
    String weakKey = "weakKey";
    Integer weakValue = 2;
      weakHashMap.put (weakKey, weakValue);

    // remove references to the keys
      key = null;
      weakKey = null;

    // force garbage collection
      System.gc ();

    // print the HashMap and WeakHashMap
      System.out.println ("HashMap: " + hashMap);	// {key=1}
      System.out.println ("WeakHashMap: " + weakHashMap);	// {}
  }
}

Output

HashMap: {key=1}
WeakHashMap: {weakKey=2}

Example 2 :Java Program to Create a weak hashmap from other maps.

Run
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.WeakHashMap;

public class Main
{
  public static void main (String[]args)
  {
    // create a HashMap and add some entries
    Map < String, Integer > hashMap = new HashMap <> ();
    hashMap.put ("one", 1);
    hashMap.put ("two", 2);
    hashMap.put ("three", 3);

    // create a Hashtable and add some entries
    Map < String, Integer > hashtable = new Hashtable <> ();
    hashtable.put ("four", 4);
    hashtable.put ("five", 5);
    hashtable.put ("six", 6);

    // create a WeakHashMap from the HashMap
    Map < String, Integer > weakHashMap = new WeakHashMap <> (hashMap);

    // add some entries to the WeakHashMap
    weakHashMap.put ("seven", 7);
    weakHashMap.put ("eight", 8);
    weakHashMap.put ("nine", 9);

    // remove references to the HashMap and Hashtable
    hashMap = null;
    hashtable = null;

    // force garbage collection
    System.gc ();

    // print the WeakHashMap
    System.out.println ("WeakHashMap: " + weakHashMap);	// {nine=9, eight=8, seven=7}
  }
}

Output

WeakHashMap: {eight=8, three=3, nine=9, one=1, seven=7, two=2}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription