Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the LinkedHashMap.merge() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

What is a LinkedHashMap?

A LinkedHashMap is the same as a HashMap, except a LinkedHashMap maintains the insertion order, whereas a HashMap doesn’t.

Internally, the LinkedHashMap uses the doubly-linked list to maintain the insertion order. Read more about LinkedHashMap here.

The merge method

The merge method of LinkedHashMap will:

  • Insert a new entry if the mapping for the key is not present.

  • If a mapping for the key is already present, then the value associated with the key will be assigned based on the value returned from the BiFunctionA functional interface that accepts two arguments and produces a result. argument of the merge method.

Syntax

public V merge(K key, V value,BiFunction function);
  • key: The key for which the value is to be added or merged.

  • value: The value to be associated with key. If there is already a value associated with that key in the map, then BiFunction recomputes the value.

  • BiFunction: A functional interface that takes two arguments, oldVal and newVal, and returns a single value.

Code

The code below demonstrates how to use the merge method:

import java.util.LinkedHashMap;
class Merge {
public static void main( String args[] ) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("Maths", 50);
map.put("Science", 60);
map.put("Programming", 70);
System.out.println( "The map is - \n" + map);
map.merge("Economics", 40, (oldVal, newVal) -> { return oldVal + newVal; });
System.out.println( "\nThe map afer executing - map.merge(Economics, 40, (oldVal, newVal) -> { return oldVal + newVal; }); \n" + map);
map.merge("Maths", 40, (oldVal, newVal) -> { return oldVal + newVal; });
System.out.println( "\n\nThe map afer executing - map.merge(Maths, 40, (oldVal, newVal) -> { return oldVal + newVal; }); \n" + map);
}
}

In the code above:

  • We create a LinkedHashMap with the name map and add entries into it.

  • We call the merge method of the LinkedHashMap object for the Economics key.

map.merge("Economics", 40, (oldVal, newVal) -> { return oldVal + newVal; });

The call to the merge method checks if the Economics key is already present on the map. In our case, the Economics key is not present in the map, so a new entry with the Economics key and value 40 will be added to the map object.

  • We again call the merge method of the LinkedHashMap object for the key Maths.
map.merge("Maths", 40, (oldVal, newVal) -> { return oldVal + newVal; });

The call to the merge method checks if the Maths key is already present on the map. In our case, the Maths key is present in the map, so the BiFunction will be executed and the value returned from the BiFunction is updated for the Maths key. In our BiFunction, we return the sum of the old value and the new value from the BiFunction.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring