Home  >  Java 8

Java Map compute() Example

By Arvind Rai, February 18, 2020
The compute is the default method of java.util.Map and has been introduced in Java 8. The compute method attempts to compute a mapping for the specified key and its current mapped value. Find the method declaration from Java doc.
default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 
The key is the key for which the specified value is to be associated.
The remappingFunction is the remapping function to compute the value.
The compute method returns new value associated with the specified key, or null if none.

The compute method works as following.
1. If old value for the specified key and new value computed by remapping function both are not null, in this case old value will be replaced by new value.
2. If old value for the specified key is not null but new value computed by remapping function is null, in this case the entry for the specified key will be deleted.
3. If old value for the specified key is null but new value computed by remapping function is not null, in this case old value will be replaced by new value.
4. If old value for the specified key is null and new value computed by remapping function is also null, in this case the entry for the specified key will be deleted.

Example-1

The old value is not null.
The new value is also not null.
In this case old value will be replaced by new value.
Compute1.java
import java.util.HashMap;
import java.util.Map;

public class Compute1 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     
     String newValue = studentMap.compute(101, (k, v) -> v + "-" + k);
     
     System.out.println(newValue);
     System.out.println(studentMap);
  }
} 
Output
Mahesh-101
{101=Mahesh-101, 102=Suresh} 
The same can be achieved without using compute method as following.
Compute11.java
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class Compute11 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     
     BiFunction<Integer, String, String> remappingFunction = (k, v) -> v + "-" + k;
     int key = 101;
     String oldValue = studentMap.get(key);
     String newValue = remappingFunction.apply(key, oldValue);
     
     if (oldValue != null) {
        if (newValue != null)
          studentMap.put(key, newValue);
        else
          studentMap.remove(key);
     } else {
        if (newValue != null)
          studentMap.put(key, newValue);
        else
          studentMap.remove(key);
     }
     
     System.out.println(newValue);
     System.out.println(studentMap);
  }
} 
Output
Mahesh-101
{101=Mahesh-101, 102=Suresh} 

Example-2

The old value is not null.
The new value is null.
In this case the entry for the specified key will be deleted.
Compute2.java
import java.util.HashMap;
import java.util.Map;

public class Compute2 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
    
     studentMap.compute(102, (k, v) -> null);
     
     System.out.println(studentMap);
  }
} 
Output
{101=Mahesh} 

Example-3

The old value is null.
The new value is not null.
In this case old value will be replaced by new value.
Compute3.java
import java.util.HashMap;
import java.util.Map;

public class Compute3 {
  public static void main(String[] args) {
     Map<String, String> map = new HashMap<>();
     map.put("Bharat", "Modi");
     map.put("Russia", null);
  
     map.compute("Bharat", (k, v) -> "Mr. ".concat(v));
     map.compute("Russia", (k, v) -> v == null ? "Mr. Putin" : "Mr. ".concat(v));
     
     System.out.println(map);
  }
} 
Output
{Bharat=Mr. Modi, Russia=Mr. Putin} 

Example-4

The old value is null.
The new value is also null.
In this case the entry for the specified key will be deleted.
Compute4.java
import java.util.HashMap;
import java.util.Map;

public class Compute4 {
  public static void main(String[] args) {
     Map<String, String> map = new HashMap<>();
     map.put("Bharat", "Modi");
     map.put("Russia", null);
  
     map.compute("Russia", (k, v) -> null);
     
     System.out.println(map);
  }
} 
Output
{Bharat=Modi} 

Reference

Java doc: Map
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us