Java HashMap compute() Method



Description

The Java HashMap compute() method is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping.

Declaration

Following is the declaration for java.util.HashMap.compute​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction) method.

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

Parameters

key − key with which the specified value is to be associated

remappingFunction − the remapping function to compute a value

Return Value

The method call returns the new value associated with the specified key, or null if none.

Exception

ConcurrentModificationException − if it is detected that the remapping function modified this map.

Compute Mapping of a HashMap of Integer, Integer Pair Example

The following example shows the usage of Java HashMap compute() method to get updated values of a Map. We've created two Map objects of Integer,Integer. Then few entries are added to map, then using compute method, the values are updated and then updated map is printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Integer> newmap = new HashMap<>();

      // populate map
      newmap.put(1, 1);
      newmap.put(2, 2);
      newmap.put(3, 3); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.compute(1, (key,value) -> value * 10);
      newmap.compute(2, (key,value) -> value * 20);
      newmap.compute(3, (key,value) -> value * 30);

      System.out.println("Updated Map: " + newmap);   
   }
}

Let us compile and run the above program, this will produce the following result.

Map: {1=1, 2=2, 3=3}
Updated Map: {1=10, 2=40, 3=90}

Compute Mapping of a HashMap of Integer, String Pair Example

The following example shows the usage of Java HashMap compute() method to get updated values of a Map. We've created two Map objects of Integer,String pair. Then few entries are added to map, then using compute method, the values are updated and then updated map is printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, String> newmap = new HashMap<>();

      // populate map
      newmap.put(1, "A");
      newmap.put(2, "B");
      newmap.put(3, "C"); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.compute(1, (key,value) -> value.concat("123"));
      newmap.compute(2, (key,value) -> value.concat("456"));
      newmap.compute(3, (key,value) -> value.concat("789"));

      System.out.println("Updated Map: " + newmap);   
   }
}

Let us compile and run the above program, this will produce the following result.

Map: {1=A, 2=B, 3=C}
Updated Map: {1=A123, 2=B456, 3=C789}

Compute Mapping of a HashMap of Integer, Student Pair Example

The following example shows the usage of Java HashMap compute() method to get a update values of a Map. We've created two Map objects of Integer,Student pair. Then few entries are added to map, then using compute method, the values are updated and then updated map is printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Student> newmap = new HashMap<>();

      // populate map
      newmap.put(1, new Student(1, "Julie"));
      newmap.put(2, new Student(2, "Robert"));
      newmap.put(3, new Student(3, "Adam"));
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.compute(1, (key,value) -> value.update("Roberts"));
      newmap.compute(2, (key,value) -> value.update("Pitts"));
      newmap.compute(3, (key,value) -> value.update("Cruise"));

      System.out.println("Updated Map: " + newmap);   
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   
   public Student update(String surname) {
	   this.name = this.name.concat(" " + surname);
	   return this;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Let us compile and run the above program, this will produce the following result.

Map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
Updated Map: {1=[ 1, Julie Roberts ], 2=[ 2, Robert Pitts ], 3=[ 3, Adam Cruise ]}
java_util_hashmap.htm
Advertisements