YouTip LogoYouTip

Java Hashmap Merge

[Image 1: Java HashMap Java HashMap](#)

\n\n

The merge() method first checks if the specified key exists. If it does not exist, it adds the key-value pair to the hashMap.

\n\n

The syntax of the merge() method is:

\n\n

hashmap.merge(key, value, remappingFunction)
\nNote: hashmap is an object of the HashMap class.

\n\n

Parameter Description:

\n\n
    \n
  • key - key
  • \n
  • value - value
  • \n
  • remappingFunction - remapping function, used to recalculate the value
  • \n
\n\n

Return Value

\n\n

If the value corresponding to the key does not exist, it returns that value. If it exists, it returns the value recalculated through the remappingFunction.

\n\n

Example

\n\n

The following example demonstrates the use of the merge() method:

\n\n

Example

\n\n
import java.util.HashMap;\n\nclass Main {\n\npublic static void main(String[] args){\n\n//Create a HashMap\n\n HashMap<String, Integer> prices =new HashMap<>();\n\n// Insert a mapping into the HashMap\n\n prices.put("Shoes", 200);\n\n prices.put("Bag", 300);\n\n prices.put("Pant", 150);\n\nSystem.out.println("HashMap: "+ prices);\n\nint returnedValue = prices.merge("Shirt", 100, (oldValue, newValue)-> oldValue + newValue);\n\nSystem.out.println("Price of Shirt: "+ returnedValue);\n\n// Print the updated HashMap\n\nSystem.out.println("Updated HashMap: "+ prices);\n\n}\n\n}
\n\n

Output of the above program:

\n\n
HashMap: {Pant=150, Bag=300, Shoes=200}\nPrice of Shirt: 100\nUpdated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}
\n\n

In the above example, we created a HashMap named prices.

\n\n

Note the expression:

\n\n
prices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue)
\n\n

In the code, we used an anonymous function lambda expression (oldValue, newValue) -> oldValue + newValue) as the remapping function.

\n\n

Because the key Shirt is not in prices, the merge() method inserts the mapping Shirt=100 into prices, and the result of the remapping function is ignored.

\n\n

To learn more about lambda expressions, visit Java Lambda Expressions.

\n\n

HashMap merge() method inserts mapping items with duplicate keys:

\n\n

Example

\n\n
import java.util.HashMap;\n\nclass Main {\n\npublic static void main(String[] args){\n\n// Create a HashMap\n\n HashMap<String, String> countries =new HashMap<>();\n\n// Insert mapping entries into the HashMap\n\n countries.put("Washington", "America");\n\n countries.put("Canberra", "Australia");\n\n countries.put("Madrid", "Spain");\n\nSystem.out.println("HashMap: "+ countries);\n\n//Merge the mapping for key Washington\n\nString returnedValue = countries.merge("Washington", "USA", (oldValue, newValue)-> oldValue +"/"+ newValue);\n\nSystem.out.println("Washington: "+ returnedValue);\n\n//Print the updated HashMap\n\nSystem.out.println("Updated HashMap: "+ countries);\n\n}\n\n}
\n\n

Output of the above program:

\n\n
HashMap: {Madrid=Spain, Canberra=Australia, Washington=America}\nWashington: America/USA \nUpdated HashMap: {Madrid=Spain, Canberra=Australia, Washington=America/USA}
\n\n

In the above example, we created a HashMap named countries.

\n\n

Note the expression:

\n\n
countries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue)
\n\n

Here, we used the lambda expression (oldValue, newValue) -> oldValue + "/" + newValue) as the remapping function.

\n\n

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.

\n\n

[Image 2: Java HashMap Java HashMap](#)

← Java Hashmap ComputeifabsentJava Hashmap Keyset β†’