[ Java HashMap](#)
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\nThe syntax of the merge() method is:
\n\nhashmap.merge(key, value, remappingFunction)
\nNote: hashmap is an object of the HashMap class.
Parameter Description:
\n\n- \n
- key - key \n
- value - value \n
- remappingFunction - remapping function, used to recalculate the value \n
Return Value
\n\nIf 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\nExample
\n\nThe following example demonstrates the use of the merge() method:
\n\nExample
\n\nimport 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\nOutput of the above program:
\n\nHashMap: {Pant=150, Bag=300, Shoes=200}\nPrice of Shirt: 100\nUpdated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}\n\nIn the above example, we created a HashMap named prices.
\n\nNote the expression:
\n\nprices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue)\n\nIn the code, we used an anonymous function lambda expression (oldValue, newValue) -> oldValue + newValue) as the remapping function.
\n\nBecause 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\nTo learn more about lambda expressions, visit Java Lambda Expressions.
\n\nHashMap merge() method inserts mapping items with duplicate keys:
\n\nExample
\n\nimport 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\nOutput of the above program:
\n\nHashMap: {Madrid=Spain, Canberra=Australia, Washington=America}\nWashington: America/USA \nUpdated HashMap: {Madrid=Spain, Canberra=Australia, Washington=America/USA}\n\nIn the above example, we created a HashMap named countries.
\n\nNote the expression:
\n\ncountries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue)\n\nHere, we used the lambda expression (oldValue, newValue) -> oldValue + "/" + newValue) as the remapping function.
\n\nBecause 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[ Java HashMap](#)
YouTip