Thursday, September 29, 2022

Java Map compute() With Examples

The compute() method in java.util.Map is added in Java 8 and used to compute a new value for the specified key.

Syntax of the compute method is as given below.

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

Parameters are as-

  • key- First parameter is the key with which the value is associated.
  • remappingFunction- This parameter is of type BiFunction functional interface and provides a function to compute new value

Method returns the new value associated with the specified key, or null if none.

The compute() method is very convenient if you want to modify value for a specified key, performs the following logic with in a one line method call.

if (oldValue != null ) {
    if (newValue != null)
       map.put(key, newValue);
    else
       map.remove(key);
 } else {
    if (newValue != null)
       map.put(key, newValue);
    else
       return null;
 }

Logic for the compute() method is as given below.

  1. If old value for the specified key is not null and the computed new value is also not null then the old value is replaced by new value for the key.
  2. If old value for the specified key is not null but the computed new value is null then the key is removed.
  3. If old value for the specified key is null and the computed new value is not null then the old value is replaced by new value for the key.
  4. If old value for the specified key is null and the computed new value is also null then return null (remove that key).

Let's try to understand all these scenarios with the help of examples.

compute() Java Examples

1. Here we have a map of items where item is the key and price is the associated value. We'll try to compute a new value for an existing key, value pair.

import java.util.HashMap;
import java.util.Map;

public class ComputeDemo {

  public static void main(String[] args) {
    Map<String, Integer> itemMap = new HashMap<>();
    itemMap.put("Trousers", 1200);
    itemMap.put("Shirt", 800);
    itemMap.put("Shoes", 2000);
    itemMap.put("Belt", 750);
    System.out.println("*** Map Initially ***");
    System.out.println(itemMap);
    // Increase price by 10%
    itemMap.compute("Trousers", (k,v) -> v + (v*10/100));
    System.out.println("*** Map After compute ***");
    System.out.println(itemMap);
  }
}

Output

*** Map Initially ***
{Shirt=800, Belt=750, Shoes=2000, Trousers=1200}
*** Map After compute ***
{Shirt=800, Belt=750, Shoes=2000, Trousers=1320}

2. In this example the remappingFunction returns null as the new value. In that case key, value pair should be removed.

public class ComputeDemo {

  public static void main(String[] args) {
    Map<String, Integer> itemMap = new HashMap<>();
    itemMap.put("Trousers", 1200);
    itemMap.put("Shirt", 800);
    itemMap.put("Shoes", 2000);
    itemMap.put("Belt", 750);
    System.out.println("*** Map Initially ***");
    System.out.println(itemMap);
    // New value as null
    itemMap.compute("Trousers", (k,v) -> null);
    System.out.println("*** Map After compute ***");
    System.out.println(itemMap);
  }
}

Output

*** Map Initially ***
{Shirt=800, Belt=750, Shoes=2000, Trousers=1200}
*** Map After compute ***
{Shirt=800, Belt=750, Shoes=2000}

As you can see "Trousers" is removed as key from our map.

3. In this example we'll have one of the items with price as null and then a new value will be computed which is not null. In that case null should be replace with the new value for the specified key.

public class ComputeDemo {

  public static void main(String[] args) {
    Map<String, Integer> itemMap = new HashMap<>();
    itemMap.put("Trousers", 1200);
    itemMap.put("Shirt", 800);
    itemMap.put("Shoes", null);
    itemMap.put("Belt", 750);
    System.out.println("*** Map Initially ***");
    System.out.println(itemMap);

    itemMap.compute("Shoes", (k,v) -> 2500);
    System.out.println("*** Map After compute ***");
    System.out.println(itemMap);
  }
}

Output

*** Map Initially ***
{Shirt=800, Belt=750, Shoes=null, Trousers=1200}
*** Map After compute ***
{Shirt=800, Belt=750, Shoes=2500, Trousers=1200}

Here you can see that the initial value for the key "Shoes" is null and the new value is computed as 2500.

4. In this example we'll have one of the items with price as null and then a new value will be computed which is also null. In that case the specified key should be removed.

public class ComputeDemo {

  public static void main(String[] args) {
    Map<String, Integer> itemMap = new HashMap<>();
    itemMap.put("Trousers", 1200);
    itemMap.put("Shirt", 800);
    itemMap.put("Shoes", null);
    itemMap.put("Belt", 750);
    System.out.println("*** Map Initially ***");
    System.out.println(itemMap);

    itemMap.compute("Shoes", (k,v) -> null);
    System.out.println("*** Map After compute ***");
    System.out.println(itemMap);
  }
}

Output

*** Map Initially ***
{Shirt=800, Belt=750, Shoes=null, Trousers=1200}
*** Map After compute ***
{Shirt=800, Belt=750, Trousers=1200}

That's all for this topic Java Map compute() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Java Map computeIfPresent() With Examples
  2. LinkedHashMap in Java With Examples
  3. Map.Entry Interface in Java
  4. How to Sort Elements in Different Order in TreeSet
  5. Fail-Fast Vs Fail-Safe Iterator in Java

You may also like-

  1. Array in Java With Examples
  2. intern() Method in Java String
  3. Try-With-Resources in Java With Examples
  4. Quick Sort Program in Java
  5. Custom Validator in Angular Reactive Form
  6. Accessing Characters in Python String
  7. Sending Email Using Spring Framework Example
  8. Spring Integration With Quartz Scheduler

No comments:

Post a Comment