Open In App

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

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

Syntax

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

Parameters

This method accepts three parameters:



Return Value

Example of the HashMap merge()

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

Example 1:




// 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);
    }
}

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: 




// 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);
    }
}

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}

Article Tags :