June 29, 2022

Java HashMap computeIfAbsent() With Examples

The Java HashMap computeIfAbsent() method is used to compute a new value for the specified key, if the specified key doesn’t already exist or key exists but mapped to null, in the HashMap.

Syntax of computeIfAbsent() method

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

The parameters are-

  1. key- Key with which the computed value has to be mapped.
  2. mappingFunction- It is an expression of type Function functional interface used to compute a value.

Method returns the value associated with the specified key, or null if the computed value is null

If the mapping function returns null, no mapping is recorded. If the mapping function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded.

The mapping function should not modify this map during computation.

computeIfAbsent() method Java examples

1. In this example a new key is specified which is not already present in the Map. For the example a HashMap is created with product name as key and price as value. Then computeIfAbsent() is used to a new (key, value) pair.

public class ComputeIfAbsentDemo {
  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    // Add new key value pair
    products.computeIfAbsent("Mouse", (key)-> 15.0);
    System.out.println("*** After Compute ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, USB=10.45, RAM=60.5}
*** After Compute ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}

2. If you try to pass any key which already exists in the HashMap then computeIfAbsent() method returns the old value and there is no change in the value. In the example key used is "Laptop" which is already present in the HashMap so there is no change in the HashMap entries.

public class ComputeIfAbsentDemo {
  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    // try to modify value for the existing key
    Double val = products.computeIfAbsent("Laptop", (key)-> 15.0);
    System.out.println("Value returned- " + val);
    System.out.println("*** After Compute ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, USB=10.45, RAM=60.5}
Value returned- 1200.0
*** After Compute ***
{Laptop=1200.0, USB=10.45, RAM=60.5}

That's all for the topic Java HashMap computeIfAbsent() With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment