Java HashMap merge() Method
The merge() method first checks whether the specified key exists. If it does not exist, it adds the key-value pair to the hashMap.
The syntax of the merge() method is:
hashmap.merge(key, value, remappingFunction)
Note:hashmap is an object of the HashMap class.
Parameter details:
- key - the key
- value - the value
- remappingFunction - the remapping function, used to recompute the value
Return Value
If the value corresponding to the key does not exist, the value is returned; if it exists, the value recomputed via remappingFunction is returned.
Example
The following example demonstrates the use of the merge() method:
Example
class Main {
public static void main(String[] args) {
// Create a HashMap
HashMap<String, Integer> prices = new HashMap<>();
// Insert mappings into the HashMap
prices.put("Shoes", 200);
prices.put("Bag", 300);
prices.put("Pant", 150);
System.out.println("HashMap: " + prices);
int returnedValue = prices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue);
System.out.println("Price of Shirt: " + returnedValue);
// Output the updated HashMap
System.out.println("Updated HashMap: " + prices);
}
}
Executing the above program produces the following output:
HashMap: {Pant=150, Bag=300, Shoes=200}
Price of Shirt: 100
Updated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}
In the above example, we created a HashMap named prices.
Note the expression:
prices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue)In the code, we used an anonymous function lambda expression (oldValue, newValue) -> oldValue + newValue) as the remapping function.
Because the key Shirt is not in prices, the merge() method inserts the mapping Shirt=100 into prices, and the execution result of the remapping function is ignored.
To learn more about lambda expressions, visitJava Lambda Expressions。
The HashMap merge() method inserts a mapping entry with a duplicate key:
Example
class Main {
public static void main(String[] args) {
// Create a HashMap
HashMap<String, String> countries = new HashMap<>();
// Insert mapping entries into the HashMap
countries.put("Washington", "America");
countries.put("Canberra", "Australia");
countries.put("Madrid", "Spain");
System.out.println("HashMap: " + countries);
// Merge the mapping with key Washington
String returnedValue = countries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue);
System.out.println("Washington: " + returnedValue);
// Output the updated HashMap
System.out.println("Updated HashMap: " + countries);
}
}
Executing the above program produces the following output:
HashMap: {Madrid=Spain, Canberra=Australia, Washington=America}
Washington: America/USA
Updated HashMap: {Madrid=Spain, Canberra=Australia, Washington=America/USA},
In the above example, we created a HashMap named countries.
Note the expression:
countries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue)
Here, we used the lambda expression(oldValue, newValue) -> oldValue + "/" + newValue)as the remapping function.
Because the key washington already exists in countries, the old value is replaced by the value returned by the remapping function. Therefore, the mapping for Washington contains America/USA.
Other extensions
Java HashMap