CodeGym /Java Blog /Java Collections /HashMap getOrDefault method in Java
Author
John Selawsky
Senior Java Developer and Tutor at LearningTree

HashMap getOrDefault method in Java

Published in the Java Collections group

What is the getOrDefault method for HashMaps in Java?

“The getOrDefault method returns the value mapped to the specified key in a HashMap. If that key is not present then a default value is returned.”
The java.util.HashMap class comes with the getOrDefault method to give an extra privilege of passing the default value. The default value can be used for different purposes in various situations. Its usage and comparison with the simple get method will be explained later in the post.

What is the header for the getOrDefault() method?

The regular header for the getOrDefault method is defined as follows.

hashMap.getOrDefault(Object key, Object defaultValue)

Parameters Passed

The method header takes two arguments. They are enlisted along with their data types below.
  1. The first one is the specified key of the Object type.
  2. The other Object type is parameter is defaultValue passed for the object key as the method argument.

Working of the getOrDefault() method

You can understand the working of the getOrDefault() method in the following two simple steps.
  1. The getOrDefault(key, defaultValue) is designed to get the value corresponding to the key in the HashMap.
  2. If there is a value associated with the key, then that value is returned. On the other hand, if the value is not available, then the defaultValue passed as a parameter to this method is returned.

Example 1


import java.util.HashMap;

public class Driver1{

	public static void main(String[] args) {

		// Declare a HashMap
		HashMap weekDays = new HashMap<>();

		// Add data to the HashMap
        weekDays.put("Monday", "Working Day");
        weekDays.put("Tuesday", "Working Day");
        weekDays.put("Wednesday", "Working Day");
        weekDays.put("Thursday", "Working Day");
        weekDays.put("Friday", "Working Day");
        weekDays.put("Saturday", "Off Day");
        weekDays.put("Sunday", "Off Day");
        
        // Print the data in the HashMap
        System.out.println("Working Schedule : " + weekDays + "\n");
              
  
        // Check if the given key is present in the Map
        // IF yes, its value will be returned
        String sunday = weekDays.getOrDefault("Sunday", "No Announcements Yet.");
        System.out.println("Is Sunday a working day?  " + sunday);
        
        // IF not, the default value passed will be returned
        String christmas = weekDays.getOrDefault("Christmas", "National Holiday");
        System.out.println("Is Christmas a working day?  " + christmas);

        // Key not present in the HashMap
        // Default Value returned
        String easter = weekDays.getOrDefault("Easter", "National Holiday");
        System.out.println("Is Easter a working day?  " + easter);
	}

}

Output

Working Schedule : {Monday=Working Day, Thursday=Working Day, Friday=Working Day, Sunday=Off Day, Wednesday=Working Day, Tuesday=Working Day, Saturday=Off Day} Is Sunday a working day? Off Day Is Christmas a working day? National Holiday Is Easter a working day? National Holiday

Why use the getOrDefault() and not get() method?

The simple get() method in Java is used to get the value of the requested key in the HashMap. If the key is found, the value is returned. In case the key is not found, “null” is returned. The getOrDefault() method is preferred over the simple get method when a default value is expected to return. Here’s a simple example for your understanding.

Example 2


import java.util.HashMap;
public class Driver2{

	public static void main(String[] args) {

	  HashMap<Object, Boolean> holidays = new HashMap<>();

	  // Add data to the HashMap
        holidays.put("Saturday",  true);
        holidays.put("Sunday", true);
        
        // Print the data in the HashMap
        System.out.println("Holidays: " + holidays + "\n");

        // Key not present, default value returned
        Object christmas = holidays.getOrDefault("Christmas", true);
        System.out.println("Is Christmas a holiday?  " + christmas);
        
        // Key not present, null returned
        christmas = holidays.get("Christmas");
        System.out.println("Is Christmas a holiday?  " + christmas);
	}
}

Output

Holidays: {Sunday=true, Saturday=true} Is Christmas a holiday? true Is Christmas a holiday? null
You can see the difference between the getOrDefault and the get method. As printed in the output, the first method returns the default value if the key is not found while the latter returns null.

Conclusion

By the end of this post, you must be familiar with a HashMap’s getOrDefault() method. You are advised to learn by practice. Feel free to check this post again if you get stuck along the way. Till then, keep practising and keep growing!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION