Java HashMap merge() Method Last Updated : 15 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Java, the merge() method of the HashMap class is used to either add new key-value pairs or update the existing one in the map by merging values using a specified function.If the key is not present in the map, the merge() method adds the key with the given value.If the key already exists in the map, the method uses a BiFunction to combine the old value and the new value into one and then updates the key with the combined value.Note: It either adds the new entry or updates an existing one by merging values togetherExample 1: The below Java program demonstrates the use of merge() method to add new key-value pairs and update the existing ones by combining old and new values using the Lambda function. Java // Java program to demonstrate adding and updating // key-value pairs using merge() import java.util.HashMap; public class Main { public static void main(String[] args) { // creating a HashMap to store key-value pairs HashMap<String, Integer> hm = new HashMap<>(); hm.put("A", 10); hm.put("B", 20); // use merge() for a key that // is not present in the map hm.merge("C", 30, (oldValue, newValue) -> oldValue + newValue); // use merge() for a key that // is already present in the map hm.merge("A", 15, (oldValue, newValue) -> oldValue + newValue); System.out.println(hm); } } Output{A=25, B=20, C=30} Syntax of HashMap merge() Methoddefault V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)Parameters: This method takes three parameters.Key: The key whose value is to be mergedValue: The new value to be merged with the existing one.remappingFunction: It is a function that takes two arguments the current value and the new value and then calculate the new valueReturn Type: Return the new value associated with the key or null if the new value is null.Example 2: The below Java program demonstrates the use of merge() to update the existing value or insert the new key-value pair using a custom BiFunction. Java // Java program to demonstrate merging // string values using merge() import java.util.HashMap; public class Geeks { public static void main(String[] args) { HashMap<String, String> hm = new HashMap<>(); hm.put("A", "Hello"); hm.put("B", "World"); // use merge() for a key that // is already present in the map hm.merge("A", "Java", (oldValue,newValue) -> oldValue + " " + newValue); // use merge() for the key that // is not present in the map hm.merge("C", "Programming", (oldValue, newValue) -> oldValue + newValue); System.out.println(hm); } } Output{A=Hello Java, B=World, C=Programming} Comment More infoAdvertise with us Next Article Java HashMap merge() Method V VincentSimon Follow Improve Article Tags : Java Java - util package Java-Functions Java-HashMap Practice Tags : Java Similar Reads Java HashMap put() Method The put() method of the Java HashMap class is used to add or update the key-value pairs in the map. If the key already exists in the map, the previous value associated with the key is replaced by the new value and If the key does not exist, the new key-value pair is added to the map.Syntax of HashMa 1 min read Java HashMap putAll() Method The putAll() method of the Java HashMap class is used to copy all key-value mappings from another map to the existing map. If a key exists in both maps, its value is updated with the value from the source map. Otherwise, new key-value pairs are added.Example 1: This example demonstrates copying all 2 min read Java HashMap replace() Method The replace() method of the HashMap class in Java is used to replace the value associated with a specific key if the key is already present in the map.Note: If the key does not exist, the method does nothing and the map remains unchanged.Example 1: This example demonstrates replacing the value of an 3 min read HashMap clear() Method in Java The clear() method of the HashMap class in Java is used to remove all of the elements or mappings (key-value pairs) from a specified HashMap.Example 2: Here, we will use the clear() method to clear a HashMap of Integer keys and String values. Java// Clearing HashMap of Integer keys // and String val 2 min read HashMap values() Method in Java The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The method i 2 min read Like