Easy Tutorial
❮ Number Exp File Rename ❯

Java HashMap merge() Method

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

The syntax for the merge() method is:

hashmap.merge(key, value, remappingFunction)

Note: hashmap is an object of the HashMap class.

Parameter Description:

Return Value

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

Example

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

import java.util.HashMap;

class Main {
    public static void main(String[] args) {
        // Create a HashMap
        HashMap&lt;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);

        // Print the updated HashMap
        System.out.println("Updated HashMap: " + prices);
    }
}

Output of the above program is:

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 a lambda expression (oldValue, newValue) -> oldValue + newValue as the remapping function.

Since 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.

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

The merge() method of HashMap to insert mappings with duplicate keys:

import java.util.HashMap;

class Main {
    public static void main(String[] args) {
        // Create a HashMap
        HashMap&lt;String, String> countries = new HashMap<>();

        // Insert mappings into the HashMap
        countries.put("Washington", "America");
        countries.put("Canberra", "Australia");
        countries.put("Madrid", "Spain");
        System.out.println("HashMap: " + countries);

        // Merge mapping for key "Washington"
        String returnedValue = countries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue);

        // Print the updated HashMap
        System.out.println("Updated HashMap: " + countries);
    }
}

Output of the above program is:

HashMap: {Canberra=Australia, Madrid=Spain, Washington=America}
Updated HashMap: {Canberra=Australia, Madrid=Spain, Washington=America/USA}

In this example, we merged the value for the key "Washington" using the merge() method.

System.out.println("Washington: " + returnedValue);

// Output the updated HashMap
System.out.println("Updated HashMap: " + countries);
}
}

The output of the above program is:

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.

Since 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 now includes America/USA.

Java HashMap ```

❮ Number Exp File Rename ❯