Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

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

WeakHashMap is a kind of Map that is implemented based on the HashTable. The WeakHashMap only stores the WeakReference type as keys. Once the key is no longer used, the respective mapping is automatically removed once the garbage collection is done.

The getOrDefault(key, defaultVaule) method of WeakHashMap will:

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

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

Syntax

public V getOrDefault(Object key,V defaultValue)

Parameters

  • key: The key whose value is to be returned.
  • defaultValue: The default mapping of key.

Return value

  • The method returns Default Value if there is no mapping for the passed key.

  • The method returns the value mapped to 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.WeakHashMap;
class DefaultVaue {
public static void main(String[] args) {
// create an WeakHashMap
WeakHashMap<Integer, String> numbers = new WeakHashMap<>();
// add items in hashmap
numbers.put(1, "One");
numbers.put(2, "Two");
numbers.put(3, "Three");
// print the items
System.out.println("The WeakHashMap 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"));
}
}

Explanation

In the code above, we:

  • Create a WeakHashMap with the name numbers.

  • Add three entries to numbers.

  • Call the get method for the key 5. This returns null because there is no value mapping for the key 5.

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

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

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