What is the WeakHashMap.computeIfAbsent() method in Java?

WeakHashMap is a type 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 computeIfAbsent() method computes a new value for a specified key, only if the key is not already present in the WeakHashMap object.

Syntax

public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

Arguments

key: The key for which the new value is to be computed.

mappingFunction: The mapping function is only called if the mapping for the key is not present.

  • This is a function that takes one argument as input and returns a value.

  • The value returned from the mappingFunction will be updated for the passed key.

  • If null is returned from the mappingFunction, then no entry will be added.

Return value

  • If the key is not present and a new entry is added when the computeIfAbsent() method is called, then the new value mapped with the specified key is returned.

  • If the key is already present, then the value associated with the specified key is returned.

Code

import java.util.WeakHashMap;
class merge {
public static void main( String args[] ) {
WeakHashMap<String, Integer> map = new WeakHashMap<>();
map.put("Maths", 50);
map.put("Science", 60);
map.put("Programming", 70);
System.out.println( "The map is - " + map);
Integer returnVal = map.computeIfAbsent("Economics", (key) ->{
System.out.println( "\nMapping function called");
return 80;
});
System.out.println("The return value from the computeIfAbsent for key Economics is " + returnVal);
System.out.println( "\nAfter calling computeIfAbsent for key Economics , map is\n" + map);
returnVal = map.computeIfAbsent("Maths", (key) ->{
System.out.println( "Mapping function called");
return 30;
});
System.out.println("\nThe return value from the computeIfAbsent for key Maths is " + returnVal);
System.out.println( "\nAfter calling computeIfAbsent for key Maths , map is\n" + map);
}
}

In the code above, we created a WeakHashMap object with the following entries:

"Maths" - 50
"Science" - 60
"Programming" - 70

We then called the computeIfAbsent() method on the created map with the Economics key. The computeIfAbsent() method will check if the Economics key is not present in the map. In our case, the Economics key is not present in the map, so the mapping function will be executed, which adds a new entry to the map with the following data:

  • key: "Economics"
  • value: The value returned from the mapping function

Now, the map will be:

"Maths" - 50
"Science" - 60
"Programming" - 70
"Economics" - 80

Next, we called the computeIfAbsent() method on the created map with the Maths key. The computeIfAbsent() method will check if the Maths key is not present in the map. In our case, the Maths key is already present in the map, so the mapping function will not be executed.

Free Resources