Open In App

Java HashMap getOrDefault() Method

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When we work with Java’s HashMap, one common problem occurs that is dealing with missing keys. If we try to fetch a value for a key that does not exist, we get “null” and sometimes it may throw NullPointerException.

To solve this problem, Java provides a method called getOrDefault() of the HashMap class. In this article, we will learn about the working of the Java HashMap getOrDefault() method.

What is getOrDefault() in HashMap?

This method getOrDefault(Object key, V defaultValue) retrieves the value associated with a given key if it exists in the map. If the key does not exist, it returns a default value provided by the user. This is helpful when we work with maps that might not always contain every possible key.

Let’s first see a basic example of getOrDefault() of HashMap.

Example 1: Retrieve the Value when the Key exists

Java
// Java Program to demonstrate the working of
// getOrDefault() with an exisiting key
import java.util.HashMap;
import java.util.Map;

public class Geeks {
    public static void main(String[] args)
    {  
        // Create a HashMap and add key-value pairs
        Map<String, Integer> hm = new HashMap<>();
        hm.put("Geek1", 1);
        hm.put("Geek2", 2);

        // Key "Geek1" exists, so its 
        // associated value is returned
        int a = hm.getOrDefault("Geek1", 0);
        System.out.println("Value for 'Geek1': " + a);
    }
}

Output
Value for 'Geek1': 1

Explanation: Geek1 exists in the map, so its associated value is returned.

Syntax of getOrDefault() Method

public V getOrDefault(Object key, V defaultValue)

V: It represents the type of the value that the map holds. It is a generic type and it will match the type of the values in the HashMap.

Parameters:

  • key: The key whose associated value is to be returned.
  • defaultValue: The value to return if the specified key is not present in the map.


Example 2: Returning a Default Value when the Key does not exists

Java
// Java Program to demonstrate the working of
// getOrDefault() with an non-exisiting key
import java.util.HashMap;
import java.util.Map;

public class Geeks {
    public static void main(String[] args)
    {
        Map<String, Integer> hm = new HashMap<>();
        hm.put("Geek1", 1);
        hm.put("Geek2", 2);

        // Key "Geek5" does not exists so 
        // the default value is returned
        int a = hm.getOrDefault("Geek5", 0);
        System.out.println("Value for 'Geek5': " + a);
    }
}

Output
Value for 'Geek5': 0

Explanation: In the above example, it does not returns “null”, it returns the default value 0.

Example 3: Key exists but the value is null

Java
// Java Program to demonstrate how to handle cases
// where the key exists with null value or does not exist
import java.util.HashMap;
import java.util.Map;

public class Geeks {
    public static void main(String[] args)
    {
        Map<String, String> hm = new HashMap<>();
        hm.put("Geek", null);

        // Key "Geek" exists but its value is null, 
        // so null is returned
        String s1 = hm.getOrDefault("Geek", "default");
        System.out.println("Value for 'Geek': " + s1);

        // Key "Geek2" does not exist, so 
        // the default value is returned
        String s2 = hm.getOrDefault("Geek2", "default");
        System.out.println("Value for 'Geek2': " + s2);
    }
}

Output
Value for 'Geek': null
Value for 'Geek2': default

Explanation: In the above example, the getOrDefault() method returns null, because the key exists but its value is explicitly set to null.


How is it better that containsKey() and get()

The traditional way to fetch values is:

String value;

if (map.containsKey(key)) {

value = map.get(key);

} else {

value = “default”;

}

The same thing can be done in one line with:

String value = map.getOrDefault(key, “default”);

Real-World Use Case

Suppose we are building a voting system that stores vote counts by candidate name,

Map<String, Integer> votes = new HashMap<>();


// If “Sweta” has not received any votes yet, return 0

int i = votes.getOrDefault(“Sweta”, 0);

votes.put(“Sweta”, i + 1);

Here, the getOrDefault() shows that the count starts at 0 even if the key is missing.

Important Points:

  • The getOrDefault() method is a simple and concise way to handle missing keys in a HashMap.
  • If the key exists but it maps to null, we will still get null.


Next Article

Similar Reads