Skip to main content

Java HashMap computeIfPresent() With Examples

The Java HashMap computeIfPresent() method is used to compute a new value for the specified key if key already exists (and non-null) in the HashMap.

Syntax of computeIfPresent() method

computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

The parameters are-
  1. key- Key with which the computed value has to be mapped.
  2. remappingFunction- It is an expression of type BiFunction functional interface used to compute a value.

Method returns a new value associated with the specified key or null if there is no new values.

If the remapping function returns null, the mapping is removed.

If the remapping function itself throws an exception, the exception is rethrown, and the current mapping is left unchanged.

The remapping function should not modify this map during computation.

computeIfPresent() method Java examples

1. In this example a new value is computed for the specified key. For the example a HashMap is created with product name as key and price as value. Then computeIfPresent() is used to change the price (value) for the specified key.

public class ConputeIfPresentDemo {

  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);
    products.put("Mouse", 15.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    products.computeIfPresent("Laptop", (key, value)-> value - (value * 20/100));
    System.out.println("*** After Compute ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}
*** After Compute ***
{Laptop=960.0, Mouse=15.0, USB=10.45, RAM=60.5}

2. If you try to pass any key which doesn’t exist in the HashMap then computeIfPresent() method just returns null and the HashMap has no change. In the example key used is “Laptop1” which is not present in the HashMap.

public class ConputeIfPresentDemo {

  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);
    products.put("Mouse", 15.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    products.computeIfPresent("Laptop1", (key, value)-> value - (value * 20/100));
    System.out.println("*** After Compute ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}
*** After Compute ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}

3. In this example we’ll check the scenario what happens if the remappingFunction returns null. In that case the (key, value) pair should be removed. To verify that the function explicitly returns null in the code.

public class ConputeIfPresentDemo {

  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);
    products.put("Mouse", 15.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    products.computeIfPresent("Laptop", (key, value)-> null);
    System.out.println("*** After Compute ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}
*** After Compute ***
{Mouse=15.0, USB=10.45, RAM=60.5}

As you can see the product “Laptop” is removed.

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


You may also like