Java HashMap merge() Method



Description

The Java HashMap merge() method is used to compute a mapping for the specified key If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, it replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key.

Declaration

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

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

Parameters

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

value − the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key.

remappingFunction − the remapping function to recompute a value if present.

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 for a Key in a HashMap of Integer, Integer Pair Example

The following example shows the usage of Java HashMap merge() method to get updated values of a Map. We've created two Map objects of Integer,Integer pair. Then few entries are added to map, then using merge 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(3, 3); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.merge(1, 1,(key,value) -> value * 10);
      newmap.merge(2, 2,(key,value) -> value * 20);
      newmap.merge(3, 3,(key,value) -> value * 30);

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

Output

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

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

Compute mapping for a Key in 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(3, "C"); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.merge(1, "A", (key,value) -> value.concat("123"));
      newmap.merge(2, "B", (key,value) -> value.concat("456"));
      newmap.merge(3, "C", (key,value) -> value.concat("789"));

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

Output

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

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

Compute mapping for a Key in 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(3, new Student(3, "Adam"));
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.merge(1, new Student(1, "Julie"), (key,value) -> value.update("Roberts"));
      newmap.merge(2, new Student(2, "Robert"), (key,value) -> value.update("Pitts"));
      newmap.merge(3, new Student(1, "Julie"), (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 + " ]";
   }
}

Output

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

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