HashMap merge(key, value, BiFunction) method in Java with Examples

The merge(Key, Value, BiFunctional) method of HashMap class is used to combine multiple mapped values for a key using the given mapping function. Bucket is actually an index of array, that array is called table in HashMap implementation. So table[0] is referred to as bucket0, table[1] as bucket1 and so on.

  • If the key is not present or is associated with null, it simply outputs the key along with the corresponding value in the Hashmap as a new entry.
  • However, if the key already holds some value that Remapping Function merges both the old and new values with the given key
  • In case key is null, it is always mapped to bucket 0, as hash is not calculated for null keys due to NullPointerException

Syntax:

public V merge(K key, V value,
    BiFunction remappingFunction)

Parameters:This method accepts three parameters:



  • Key: which is the key for which we have a particular value. If two keys have the same value they are merged.
  • Value: which is the index corresponding to the particular key which is stored in the bucket.
  • BiFunction: which is the function having two arguments to be used for calculating the new mapping from the old value and given value.

Return Value: This method returns the key along with its value if the key is not present or is associated with null. ELse if the key already holds any value, it merges the old value with the new value using the mapping technique.

Below programs illustrate the merge(Key, Value, BiFunctional) method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// computeIfAbsent(Key, Function) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a HashMap and add some values
        HashMap<Integer, String>
            map1 = new HashMap<>();
        map1.put(1, "L");
        map1.put(2, "M");
        map1.put(3, "N");
  
        HashMap<Integer, String>
            map2 = new HashMap<>();
        map2.put(1, "B");
        map2.put(2, "G");
        map2.put(3, "R");
  
        // print map details
        System.out.println("HashMap1: "
                           + map1.toString());
  
        System.out.println("HashMap2: "
                           + map2.toString());
  
        // provide value for new key which is absent
        // using computeIfAbsent method
        map2.forEach(
            (key, value)
                -> map1.merge(
                    key,
                    value,
                    (v1, v2)
                        -> v1.equalsIgnoreCase(v2)
                               ? v1
                               : v1 + ", " + v2));
  
        // print new mapping
        System.out.println("New HashMap: " + map1);
    }
}

chevron_right


Output:

HashMap1: {1=L, 2=M, 3=N}
HashMap2: {1=B, 2=G, 3=R}
New HashMap: {1=L, B, 2=M, G, 3=N, R}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// computeIfAbsent(Key, Function) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a HashMap and add some values
        HashMap<Integer, String>
            map1 = new HashMap<>();
        map1.put(1, "Ram");
        map1.put(2, "Rohan");
        map1.put(3, "Shivam");
  
        HashMap<Integer, String>
            map2 = new HashMap<>();
        map2.put(1, "Tushar");
        map2.put(10, "Satya");
        map2.put(12, "Sundar");
  
        // print map details
        System.out.println("HashMap1: "
                           + map1.toString());
  
        System.out.println("HashMap2: "
                           + map2.toString());
  
        // provide value for new key which is absent
        // using computeIfAbsent method
        map2.forEach(
            (key, value)
                -> map1.merge(
                    key,
                    value,
                    (v1, v2)
                        -> v1.equalsIgnoreCase(v2)
                               ? v1
                               : v1 + ", " + v2));
  
        // print new mapping
        System.out.println("New HashMap: " + map1);
    }
}

chevron_right


Output:

HashMap1: {1=Ram, 2=Rohan, 3=Shivam}
HashMap2: {1=Tushar, 10=Satya, 12=Sundar}
New HashMap: {1=Ram, Tushar, 2=Rohan, 3=Shivam, 10=Satya, 12=Sundar}

References: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#merge-K-V-java.util.function.BiFunction-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.