Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

weakhashmap
isempty
java
communitycreator

What is the WeakHashMap.isEmpty method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

WeakHashMap is a kind of Map implemented based on the HashTable. The WeakHashMap only stores WeakReference type as their keys. Once the key is no longer used, the respective mapping will be automatically removed when the garbage collection is done.

The isEmpty method of WeakHashMap is used to check if the specified WeakHashMap object is emptyno mapping present.

Syntax

public boolean isEmpty()

This method doesn’t take any argument.

This method returns true if the map doesn’t have any mappings. Otherwise, false will be returned.

Code

The following example shows how to use the isEmpty method.

import java.util.WeakHashMap;
class WeakHashMapisEmptyExample {
public static void main( String args[] ) {
WeakHashMap<String, Integer> map = new WeakHashMap<>();
String key = new String("ten");
map.put(key, 10);
System.out.println("\nChecking if WeakHashMap is empty : "+ map.isEmpty());
key = null;
System.gc();
// calling gc doesn't guarantee that it will execute Garbage Collection
// so make sure to click `run` multiple times
System.out.println("\nAfter Adding some mappings to the map");
System.out.println("\nChecking if WeakHashMap is empty : "+ map.isEmpty());
}
}
Implementing the isEmpty() method

Explanation

In the code above:

  • We imported the WeakHashMap class.

  • We created a WeakHashMap object with the name Map.

  • We created a new String object with the name key. Refer here for using immutable keys in WeakHashMap.

  • We added one mapping (ten-10) to the map.

  • We used the isEmpty method to check if the Map is empty. false is returned as a result because the Map object contains one mapping.

  • We assigned null as a value to the key object and called the Garbage collector using the System.gc method. The garbage collector will remove the object from the memory. Due to this, the mapping present in the Map for the key will also be removed automatically. Then, we used the isEmpty method to check if the Map is empty. true is returned as a result because the Map object contains no mapping.

Calling System.gc() doesn’t guarantee that it will execute garbage collection, so make sure to click run multiple times.

RELATED TAGS

weakhashmap
isempty
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring