Open In App

Java HashMap merge() Method

Last Updated : 15 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Java, the merge() method of the HashMap class is used to either add new key-value pairs or update the existing one in the map by merging values using a specified function.

  • If the key is not present in the map, the merge() method adds the key with the given value.
  • If the key already exists in the map, the method uses a BiFunction to combine the old value and the new value into one and then updates the key with the combined value.

Note: It either adds the new entry or updates an existing one by merging values together

Example 1: The below Java program demonstrates the use of merge() method to add new key-value pairs and update the existing ones by combining old and new values using the Lambda function.

// Java program to demonstrate adding and updating 
// key-value pairs using merge()
import java.util.HashMap;

public class Main {
    public static void main(String[] args)
    {
        // creating a HashMap to store key-value pairs
        HashMap<String, Integer> hm = new HashMap<>();
        hm.put("A", 10);
        hm.put("B", 20);

        // use merge() for a key that 
        // is not present in the map
        hm.merge("C", 30,
            (oldValue, newValue) -> oldValue + newValue);

        // use merge() for a key that 
        // is already present in the map
        hm.merge("A", 15,
            (oldValue, newValue) -> oldValue + newValue);

        System.out.println(hm);
    }
}

Output
{A=25, B=20, C=30}

Syntax of HashMap merge() Method

default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)

Parameters: This method takes three parameters.

  • Key: The key whose value is to be merged
  • Value: The new value to be merged with the existing one.
  • remappingFunction: It is a function that takes two arguments the current value and the new value and then calculate the new value

Return Type: Return the new value associated with the key or null if the new value is null.

Example 2: The below Java program demonstrates the use of merge() to update the existing value or insert the new key-value pair using a custom BiFunction.

// Java program to demonstrate merging 
// string values using merge()
import java.util.HashMap;

public class Geeks {
    public static void main(String[] args)
    {
      
        HashMap<String, String> hm = new HashMap<>();
        hm.put("A", "Hello");
        hm.put("B", "World");

        // use merge() for a key that 
        // is already present in the map
        hm.merge("A", "Java",
                (oldValue,newValue) -> oldValue + " " + newValue);

        // use merge() for the key that
        // is not present in the map
        hm.merge("C", "Programming",
            (oldValue, newValue) -> oldValue + newValue);

        System.out.println(hm);
    }
}

Output
{A=Hello Java, B=World, C=Programming}


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg