The HashMap.merge()
method in Java is used to combine a specified value with the existing 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 into the map.
Table of Contents
- Introduction
merge
Method Syntax- Examples
- Merging Values in a HashMap
- Real-World Use Case: Updating Product Quantities
- Conclusion
Introduction
The HashMap.merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
method is a member of the HashMap
class in Java. It allows you to combine a value associated with a specified key using a remapping function. If the key is not already present, the method inserts the key-value pair.
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 typeK
, which represents the key whose associated value is to be merged.value
of typeV
, which represents the value to be merged with the existing value.remappingFunction
of typeBiFunction<? 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 isnull
.
Examples
Merging Values in a HashMap
The merge
method can be used to combine values associated with a key in a HashMap
.
Example with Lambda Expression
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}
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 with Lambda Expression
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}
Conclusion
The HashMap.merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
method in Java provides a way to combine a specified value with the 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.