Java HashMap merge() Method

The HashMap.merge() method in Java is used to merge a specified value with an existing value associated with a key in the HashMap using a given remapping function. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. merge Method Syntax
  3. Examples
    • Merging Values in a HashMap
    • Real-World Use Case: Updating Product Quantities
  4. Conclusion

Introduction

The HashMap.merge() method is a member of the HashMap class in Java. It allows you to merge a value associated with a specified key using a given remapping function. If the key is not already present, the method inserts the key-value pair; otherwise, it merges the existing value with the new value using the remapping function.

merge() Method Syntax

The syntax for the merge method is as follows:

public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
  • The method takes three parameters:
    • key of type K, which represents the key whose associated value is to be merged.
    • value of type V, which represents the value to be merged with the existing value.
    • remappingFunction of type BiFunction<? super V, ? super V, ? extends V>, which represents the function to compute the merged value.
  • The method returns the new value associated with the specified key, or null if the result of the remapping function is null.

Examples

Merging Values in a HashMap

The merge method can be used to merge values associated with a key in a HashMap.

Example

import java.util.HashMap;
import java.util.function.BiFunction;

public class MergeExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> people = new HashMap<>();

        // Adding entries to the HashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);

        // Using merge method to update the age of "Ravi"
        people.merge("Ravi", 5, (oldValue, newValue) -> oldValue + newValue);

        // Using merge method to add a new entry for "Vijay"
        people.merge("Vijay", 35, (oldValue, newValue) -> oldValue + newValue);

        // Printing the updated HashMap
        System.out.println("Updated HashMap: " + people);
    }
}

Output:

Updated HashMap: {Ravi=30, Priya=30, Vijay=35}

Explanation:

  1. The code creates a HashMap called people with String keys and Integer values.
  2. It adds two entries to the map: Ravi with a value of 25 and Priya with a value of 30.
  3. The merge method is used to update the value for the key Ravi. The remapping function (oldValue, newValue) -> oldValue + newValue adds 5 to the existing value 25, resulting in 30.
  4. The merge method is also used to add a new entry for the key Vijay. Since Vijay is not already present in the map, the value 35 is directly inserted.
  5. The final HashMap is printed, showing the updated values.

Real-World Use Case: Updating Product Quantities

In a real-world scenario, you might use the merge method to update the quantities of products in an inventory system.

Example

import java.util.HashMap;
import java.util.function.BiFunction;

public class UpdateProductQuantities {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (product IDs) and Integer values (quantities)
        HashMap<String, Integer> productInventory = new HashMap<>();

        // Adding entries to the HashMap
        productInventory.put("P001", 10);
        productInventory.put("P002", 15);

        // Using merge method to add more quantity to "P001"
        productInventory.merge("P001", 5, (oldValue, newValue) -> oldValue + newValue);

        // Using merge method to add a new product "P003"
        productInventory.merge("P003", 20, (oldValue, newValue) -> oldValue + newValue);

        // Printing the updated product inventory
        System.out.println("Updated Product Inventory: " + productInventory);
    }
}

Output:

Updated Product Inventory: {P001=15, P002=15, P003=20}

Explanation:

  1. The code creates a HashMap called productInventory with String keys and Integer values.
  2. It adds two entries to the map: P001 with a value of 10 and P002 with a value of 15.
  3. The merge method is used to update the quantity for the product P001. The remapping function (oldValue, newValue) -> oldValue + newValue adds 5 to the existing quantity 10, resulting in 15.
  4. The merge method is also used to add a new product P003 with a quantity of 20.
  5. The final HashMap is printed, showing the updated product quantities.

Conclusion

The HashMap.merge() method in Java provides a way to merge a specified value with an existing value associated with a key using a given remapping function. By understanding how to use this method, you can efficiently manage updates and conditional modifications to the values in your map. This method is useful in various scenarios, such as updating inventory quantities, handling state transitions, and managing complex data structures in collections.

Comments