Java HashMap getOrDefault Method

Java getOrDeafult ()

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.

Java getOrDefault Method

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

Example 2 : Java HashMap getOrDefault() Method

Run

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription