Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linkedhashmap
java
communitycreator

What is the LinkedHashMap.putIfAbsent 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.

A LinkedHashMap is the same as a HashMap except the LinkedHashMap maintains the insertion order, whereas the HashMap doesn’t. Internally, the LinkedHashMap uses the doubly-linked list to maintain the insertion order.

Read more about LinkedHashMap here.

The putIfAbsent method adds the key-value pair to the LinkedHashMap object if the key is not present in the map. If the key is already present, then it skips the operation.

Syntax

public V putIfAbsent(K key,V value)

If the key is already present in the map, then the value associated with the key is returned, and no insertion will be done.

If the key is not present in the map, then the key-value pair is inserted and the null is returned.

The null return can also denote that the map previously associated the null value with a key.

Code

The code below demonstrates how to use the putIfAbsent method.

import java.util.LinkedHashMap;
class Main {
public static void main( String args[] ) {
// create a LinkedHashMap
LinkedHashMap<Integer, String> numbers = new LinkedHashMap<>();
// add key-value pairs to the LinkedHashMap
numbers.put(1, "one");
numbers.put(2, "Two");
numbers.put(3, "three");
System.out.println("The map is => " + numbers);
System.out.println("\nTrying to add (1, \"ONE\") in the map ");
String value = numbers.putIfAbsent(1, "ONE");
// key - 1 is already present in the map
// so the value associated with the key is returned
System.out.println("The value is => " + value);
System.out.println("\nTrying to add (4, \"Four\") in the map ");
value = numbers.putIfAbsent(4, "FOUR");
// key - 4 is not already present in the map
// so a new entry with the key as 4 and value as FOUR will be added
// and null is returned
System.out.println("The value is => " + value);
System.out.println("The map is => " + numbers);
}
}

In the code above:

  • We created a LinkedHashMap with three entries.

  • We attempted to add a new entry to the LinkedHashMap with:

    1. The already existing key 1. In this case, the old value will be returned and the insertion will be skipped.
    2. The new key 4. In this case, the new key-value entry will be inserted and null is returned.

RELATED TAGS

linkedhashmap
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