Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
doubly
linkedlist

What is the LinkedHashMap.getOrDefault 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 a doubly-linked list to maintain the insertion order.

Syntax

public V getOrDefault(Object key,V defaultValue)

Parameters

  • key: Key whose value is to be returned.
  • defaultValue: Default mapping of key.

Return value

  • Returns Default Value if there is no mapping for the passed key.

  • Returns the value mapped to the key if there is a mapping present for the passed key.

Code

The code below demonstrates how to use the getOrDefault method.

import java.util.LinkedHashMap;
class DefaultVaue {
public static void main(String[] args) {
// create an LinkedHashMap
LinkedHashMap<Integer, String> numbers = new LinkedHashMap<>();
numbers.put(1, "One");
numbers.put(2, "Two");
numbers.put(3, "Three");
System.out.println("The LinkedHashMap is - " + numbers);
System.out.print("\nGetting value for key 5 using get(5) Method :" );
System.out.println(numbers.get(5));
System.out.print("\nGetting value for key 5 using getOrDefault(5, 'Default Value') Method :" );
System.out.println(numbers.getOrDefault(5, "Default value"));
System.out.print("\nGetting value for key 1 using getOrDefault Method :" );
System.out.println(numbers.getOrDefault(1, "Default value"));
}
}

In the code above, we:

  • Created a LinkedHashMap with the name numbers.

  • Added three entries to the numbers.

  • Called the get method for the key 5. This will return null because there is no value mapping for the key 5.

  • Called the getOrDefault(5, "Default Value") method. This will return Default Value because there is no value mapped for the key 5, so the default value return is passed.

  • Called the getOrDefault(1, "Default Value") method. This will return One because the value One is mapped for the key 1.

For example, say we have a mapping for users with images. If the image for the user is not present, then it returns the default user image. Cases like this can be handled with the getOrDefault method.

Use the getOrDefault method when you need a default value if the value is not present in the LinkedHashMap.

RELATED TAGS

java
communitycreator
doubly
linkedlist

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