User Brian
Brian
Level 41

HashMap getOrDefault method in Java

Published in the Java Developer group

What is the HashMap getOrDefault method in Java?

“The getOrDefault method is used to return the available or the default value of the search key.”
This method is available if you work with the HashMap class that is implemented by the Map interface in Java. If you do not have prior knowledge of HashMaps it is recommended to learn before we proceed.

Method Header

Here is a regular header for the getOrDefault method.

hashMap.getOrDefault(Object key, Object defaultValue)
Kindly note that “defaultValue” is passed for the object “key” as a parameter.

How does getOrDefault() work?

Here are 2 simple steps for an amateur to understand its procedure.
  1. The getOrDefault(key, defaultValue) method is used to “get” the “value” associated with the “key” passed as a parameter in the HashMap.

  2. If a corresponding value to the key is available, that “value” is returned. Contrarily, if the value is not present, 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<String, String> 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 getOrDefault method is preferred over the simple get method when a default value is expected to return. Whereas, a get method returns “null” if the key is not found. 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 practicing and keep growing! HashMap getOrDefault method in Java - 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION