Java HashMap getOrDefault Method
What is Java HashMap getOrDefault Method?
- Java getOrDefault() is a method in the Java HashMap class that returns the value to which the specified key is mapped, or a default value if the key is not present in the HashMap.
- The method takes two parameters:
- key – the key whose associated value is to be returned
- defaultValue – the default value to be returned if the key is not present in the HashMap
- To understand the Java LinkedBlockingQueue, Read the Complete Article.
The Java HashMap getOrDefault() method performs the following operations:
The Java getOrDefault() method performs the following operations:
- It takes two parameters: a key and a default value.
- If the key is present in the Map, it returns the value associated with the key.
- If the key is not present in the Map, it returns the default value.
- The default value is optional, and if not provided, it defaults to null.
The Java getOrDefault() method is useful when you want to retrieve a value from a Map but don’t know if the key is present in the Map. It allows you to specify a default value that will be returned if the key is not present in the Map, which can help you avoid null pointer exceptions.
getOrDefault() Parameters:
The getOrDefault() method in Java takes two parameters:
key: the key whose associated value is to be returned.
defaultValue: the default value to be returned if the key is not present in the map.
The first parameter, key, is the key whose value is to be retrieved from the map. If the key is present in the map, then the value associated with the key is returned. If the key is not present in the map, then the default value specified in the second parameter is returned.
The second parameter, defaultValue, is the default value to be returned if the key is not present in the map. This parameter is optional and can be set to null if no default value is required.
getOrDefault() Return Value:
The getOrDefault() method in Java returns the value to which the specified key is mapped, or a default value if the key is not present in the map.
If the key is present in the map, then the method returns the value associated with the key.
If the key is not present in the map, then the default value specified in the second parameter of the method is returned.
Let’s look at the Java getOrDefault() Method to perform certain operations.
Example 1: Java Program to getOrDefault() Method
Run
import java.util.HashMap; import java.util.Map; public class Main { public static void main (String[]args) { Map < String, Integer > map = new HashMap <> (); map.put ("apple", 1); map.put ("banana", 2); // Retrieving the value associated with an existing key int appleValue = map.getOrDefault ("apple", 0); // returns 1 // Retrieving the value associated with a non-existing key int orangeValue = map.getOrDefault ("orange", 0); // returns 0 System.out.println ("Value of apple: " + appleValue); System.out.println ("Value of orange: " + orangeValue); } }
Output
Value of apple: 1 Value of orange: 0
Explanation:
In the above example, we create a HashMap and add two key-value pairs to it.
We then use the getOrDefault() method to retrieve the values associated with two keys: "apple" (which is present in the map) and "orange" (which is not present in the map).
The method returns the value associated with "apple" (which is 1) and the default value 0 for "orange".
Example 2 : Java HashMap getOrDefault() Method
import java.util.HashMap; import java.util.Map; public class Main { public static void main (String[]args) { Map < String, Double > grades = new HashMap <> (); grades.put ("Ashish", 85.0); grades.put ("Sarina", 90.5); grades.put ("David", 92.3); // retrieve the grade for an existing student Double AshishGrade = grades.getOrDefault ("Ashish", 0.0); System.out.println ("Ashish's grade: " + AshishGrade); // retrieve the grade for a non-existing student Double sarinaGrade = grades.getOrDefault ("sarina", 0.0); System.out.println ("sarina's grade: " + sarinaGrade); } }
Output
Ashish's grade: 85.0 sarina's grade: 0.0
Explanation:
In the above example, we create a HashMap called grades and add three key-value pairs to it, where the keys are student names and the values are their grades.
We then use the getOrDefault() method to retrieve the grades of two students: "Ashish" (which is present in the map) and "Sarina" (which is not present in the map).
The method returns the grade associated with "Ashish" and the default value 0.0 for "sarina".
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment