September 20, 2023

Java HashMap merge() With Examples

The Java HashMap merge() method is used to insert a new (key,value) pair into the HashMap or to modify value for an already existing key.

Syntax of Java HashMap merge() method

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

The parameters are-

  1. key- Key with which the resulting value has to be mapped.
  2. value- The non-null value which replaces the existing value associated with the key or inserts this new value.
  3. remappingFunction- It is an expression of type BiFunction functional interface used to recompute a value if present

merge method has the following scenarios-

  1. If the specified key is not already associated with a value associates it with the given non-null value and the (key, value) pair is inserted into the HashMap.
  2. If the specified key is associated with null value, associates it with the given non-null value.
  3. If the key already exists then replaces (or merges) the associated value with the results of the given remapping function.
  4. If the result of the given remapping function is null then removes the (key, value) pair from the HashMap.

Merge() Java examples

1. In this example a value is recomputed for the specified key. For the example a HashMap is created with product name as key and price as value. Then merge() is used to change the price (value) for the specified key.

public class MapMergeDemo {

  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    products.put("Mouse", 15.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    // recomputing price
    products.merge("Laptop", 1200.0, (oldValue, newValue)-> oldValue- (oldValue*20/100));
    System.out.println("*** After Merge ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}
*** After Merge ***
{Laptop=960.0, Mouse=15.0, USB=10.45, RAM=60.5}

2. Inserting a new entry into the HashMap using merge method.

public class MapMergeDemo {
  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    products.put("Mouse", 15.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    // Adding new key
    products.merge("Adapter", 5.0, (oldValue, newValue)-> oldValue + newValue);
    System.out.println("*** After Merge ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5}
*** After Merge ***
{Laptop=1200.0, Mouse=15.0, USB=10.45, Adapter=5.0, RAM=60.5}

3. If key exists but is associated with null value then value can be changed to a new value using merge() method . In the example HashMap has one key with associated value as null.

public class MapMergeDemo {
  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    products.put("Mouse", null);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    // remappingFunction returns null
    products.merge("Mouse", 12.50, (oldValue, newValue)-> oldValue+newValue);
    System.out.println("*** After Merge ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=null, USB=10.45, RAM=60.5}
*** After Merge ***
{Laptop=1200.0, Mouse=12.5, USB=10.45, RAM=60.5}

4. In this example we’ll see the scenario where remappingFunction of the merge() method returns null. In that case the (key, value) pair should be removed. To verify that the function explicitly returns null in the code.

public class MapMergeDemo {

  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    products.put("Mouse", 8.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    // remapping function returns null
    products.merge("Laptop", 1200.0, (oldValue, newValue)-> null);
    System.out.println("*** After Merge ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=8.0, USB=10.45, RAM=60.5}
*** After Merge ***
{Mouse=8.0, USB=10.45, RAM=60.5}

5. In this example we’ll see the merging of old value and new value. Since the remapping function gets both old and new value as arguments so we can have logic to compute value using both old and new value. In the product HashMap if price of Laptop has to be increased by 20.

public class MapMergeDemo {

  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    products.put("Mouse", null);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    products.merge("Laptop", 20.0, (oldValue, newValue)-> oldValue+newValue);
    System.out.println("*** After Merge ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=null, USB=10.45, RAM=60.5}
*** After Merge ***
{Laptop=1220.0, Mouse=null, USB=10.45, RAM=60.5}

6. Using merge() method to recompute all values in the HashMap. In the products HashMap if you want to increase price by 10% for all the products.

public class MapMergeDemo {

  public static void main(String[] args) {
    Map<String, Double> products = new HashMap<>();
    products.put("Laptop", 1200.0);
    products.put("RAM", 60.50);
    products.put("USB", 10.45);
    products.put("Mouse", 8.0);
    System.out.println("*** Initial Values ***");
    System.out.println(products);
    products.forEach((k,v) -> products.merge(k, v, (oldValue, newValue)-> oldValue+ (oldValue * 10/100)));
    System.out.println("*** After Merge ***");
    System.out.println(products);
  }
}
Output
*** Initial Values ***
{Laptop=1200.0, Mouse=8.0, USB=10.45, RAM=60.5}
*** After Merge ***
{Laptop=1320.0, Mouse=8.8, USB=11.495, RAM=66.55}

Here forEach is used to iterate the Map and then merge() method is used to increase value by 10%.

That's all for the topic Java HashMap merge() With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment