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
- Introduction
merge
Method Syntax- Examples
- Merging Values in a HashMap
- Real-World Use Case: Updating Product Quantities
- 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 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 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:
- The code creates a
HashMap
calledpeople
withString
keys andInteger
values. - It adds two entries to the map:
Ravi
with a value of25
andPriya
with a value of30
. - The
merge
method is used to update the value for the keyRavi
. The remapping function(oldValue, newValue) -> oldValue + newValue
adds5
to the existing value25
, resulting in30
. - The
merge
method is also used to add a new entry for the keyVijay
. SinceVijay
is not already present in the map, the value35
is directly inserted. - 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:
- The code creates a
HashMap
calledproductInventory
withString
keys andInteger
values. - It adds two entries to the map:
P001
with a value of10
andP002
with a value of15
. - The
merge
method is used to update the quantity for the productP001
. The remapping function(oldValue, newValue) -> oldValue + newValue
adds5
to the existing quantity10
, resulting in15
. - The
merge
method is also used to add a new productP003
with a quantity of20
. - 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
Post a Comment
Leave Comment