English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
If the specified key does not exist, the Java HashMap merge() method will add the specified key/The value mapping is inserted into the hash map.
However, if the specified key is already associated with a value, this method will replace the old value with the result of the specified function.
The syntax of the merge() method is:
hashmap.merge(key, value, remappingFunction)
The merge() method uses3Parameters:
key - To specify the value associated with the key
value - If the key is already associated with any value, then the value associated with the key
remappingFunction - If the key is already associated with a value, the result is associated with the key.
Returns the new value associated with the key (key)
If there is no value associated with the key (key), then null is returned
NoteIf the remappingFunction result is null, the mapping for the specified key will be deleted.
import java.util.HashMap; class Main { public static void main(String[] args) { //Create HashMap HashMap<String, Integer> prices = new HashMap<>(); //Insert entries into 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) -> old value + newValue); System.out.println("The price of the shirt: ", + returnedValue); //Print the updated HashMap System.out.println("Updated HashMap: ") + prices); } }
Output result
HashMap: {Pant=150, Bag=300, Shoes=200} The price of the shirt: 100 Updated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}
In the above example, we created a hash map named prices. Note the expression
prices.merge("Shirt", 100, (oldValue, newValue) -> old value + newValue)
Here, we use the lambda expression (oldValue, newValue) -> oldValue + newValue) as the remappingFunction function. For more information on lambda expressions, please visitJava Lambda Expressions.
Since the key Shirt does not exist in prices, the merge() method will be Shirt=100 insert the mapping. And, the result of remappingFunction will be ignored.
import java.util.HashMap; class Main { public static void main(String[] args) { // Create HashMap HashMap<String, String> countries = new HashMap<>(); // Insert entries into HashMap countries.put("Washington", "America"); countries.put("Canberra", "Australia"); countries.put("Madrid", "Spain"); System.out.println("HashMap: ") + countries); // Merge mapping for the Washington key String returnedValue = countries.merge("Washington", "USA", (oldValue, newValue) -> old value + "/" + newValue); System.out.println("Washington: ") + returnedValue); // Print the updated HashMap System.out.println("Updated HashMap: ") + countries); } }
Output result
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 hash map named countries. Note the expression
countries.merge("Washington", "USA", (oldValue, newValue) -> old value + "/" + newValue)
Here, we used the lambda expression (oldValue, newValue) -> oldValue + "/" + as remappingFunction.
Since the key Washington already exists in countries, the old value will be replaced by the value returned by remappingFunction. Therefore, the mapping of Washington includes the value America/USA.
import java.util.HashMap; class Main { public static void main(String[] args) { //Create HashMap HashMap<String, Integer> prices1 = new HashMap<>(); //Insert entries into HashMap prices1.put("Pant", 230); prices1.put("Shoes", 350); System.out.println("HashMap 1: \ + prices1); //Create another hashmap HashMap<String, Integer> prices2 = new HashMap<>(); //Insert entries into HashMap prices2.put("Shirt", 150); prices2.put("Shoes", 320); System.out.println("HashMap 2: \ + prices2); // forEach() accesses prices2each entry // merge() merges each entry from prices2Insert into prices1 prices2.forEach((key, value -> prices1.merge(key, value, (oldValue, newValue) -> { //Return the smaller value if (oldValue < newValue) { return oldValue; } else { return newValue; } }); System.out.println("The merged HashMap: ", + prices1); } }
Output result
HashMap 1: {Pant=230, Shoes=350} HashMap 2: {Shirt=150, Shoes=320} The merged HashMap: {Pant=230, Shirt=150, Shoes=320}
In the above example, we created two hash maps named prices1and prices2hash mapping. Note the code,
prices2.forEach((key, value -> prices1.merge(key, value, (oldValue, newValue) -> { if (oldValue < newValue) { return oldValue; } else { return newValue; } });
Here,HashMap.forEach()method to access the hash table prices2each entry, and merge it into the hash table prices1In this case, we used two lambda expressions:
(key, value) -> prices.merge(...) - It accesses prices1of each entry and pass it to the merge() method.
(oldValue, newValue) -> {...} - This is a remapping function (remappingFunction). It compares two values and returns the smaller value.
Since the key Shoes exists in both hash maps, the value of Shoes is replaced by the result of the remapping function (remappingFunction).
We can also use the putAll() method to merge two hash maps. However, if both hash maps have existing keys, the old value will be replaced by the new value
Unlike merge(), the putAll() method does not provide remapping functionality. Therefore, we cannot determine the values to be stored for repeated keys.
For more information about the putAll() method, please visitJava HashMap putAll().