Open In App

HashMap getOrDefault(key, defaultValue) method in Java with Examples

Last Updated : 15 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The getOrDefault(Object key, V defaultValue) method of Map interface, implemented by HashMap class is used to get the value mapped with specified key. If no value is mapped with the provided key then the default value is returned.

Syntax:

default V getOrDefault(Object key, V defaultValue)

Parameters: This method accepts two parameters:

  • key: which is the key of the element whose value has to be obtained.
  • defaultValue: which is the default value that has to be returned, if no value is mapped with the specified key.

Return Value: This method returns value mapped with the specified key, otherwise default value is returned.

Program 1:




// Java program to demonstrate
// getOrDefault(Object key, V defaultValue) method
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Create a HashMap and add some values
        HashMap<String, Integer> map
            = new HashMap<>();
        map.put("a", 100);
        map.put("b", 200);
        map.put("c", 300);
        map.put("d", 400);
  
        // print map details
        System.out.println("HashMap: "
                           + map.toString());
  
        // provide key whose value has to be obtained
        // and default value for the key. Store the
        // return value in k
        int k = map.getOrDefault("b", 500);
  
        // print the value of k returned by
        // getOrDefault(Object key, V defaultValue) method
        System.out.println("Returned Value: " + k);
    }
}


Output:

HashMap: {a=100, b=200, c=300, d=400}
Returned Value: 200

Program 2:




// Java program to demonstrate
// getOrDefault(Object key, V defaultValue) method
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Create a HashMap and add some values
        HashMap<String, Integer> map
            = new HashMap<>();
        map.put("a", 100);
        map.put("b", 200);
        map.put("c", 300);
        map.put("d", 400);
  
        // print map details
        System.out.println("HashMap: "
                           + map.toString());
  
        // provide key whose value has to be obtained
        // and default value for the key. Store the
        // return value in k
        int k = map.getOrDefault("y", 500);
  
        // print the value of k returned by
        // getOrDefault(Object key, V defaultValue) method
        System.out.println("Returned Value: " + k);
    }
}


Output:

HashMap: {a=100, b=200, c=300, d=400}
Returned Value: 500

References: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#getOrDefault-java.lang.Object-V-



Similar Reads

Properties getOrDefault(key, defaultValue) method in Java with Examples
The getOrDefault(key, defaultValue) method of Properties class is used to get the value mapped to this key, passed as the parameter, in this Properties object. This method will fetch the corresponding value to this key, if present, and return it. If there is no such mapping, then it returns the defaultValue. Syntax: public Object getOrDefault(Objec
2 min read
Properties getProperty(key, defaultValue) method in Java with Examples
The getProperty(key, defaultValue) method of Properties class is used to get the value mapped to this key, passed as the parameter, in this Properties object. This method will fetch the corresponding value to this key, if present, and return it. If there is no such mapping, then it returns the defaultValue. Syntax: public String getProperty(String
2 min read
How to Copy One HashMap to Another HashMap in Java?
HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.util.HashMap package or its superclass
4 min read
Converting Integer-String HashMap to String-Array-Integer HashMap in Java
Efficient Conversion of a HashMap&lt;Integer, String&gt; into another HashMap&lt;String, Integer[]&gt; involves organizing integer keys based on their corresponding string values. Here's a concise and efficient Java method to accomplish this transformation, focusing on clear and straightforward implementation. Step-by-Step Implementation of Convers
3 min read
HashMap replace(key, oldValue, newValue) method in Java with Examples
The replace(K key, V oldValue, V newValue) method of Map interface, implemented by HashMap class is used to replace the old value of the specified key only if the key is previously mapped with the specified old value. Syntax: default boolean replace(K key, V oldValue, V newValue) Parameters: This method accepts three parameters : key: which is the
3 min read
HashMap replace(key, value) method in Java with Examples
The replace(K key, V value) method of Map interface, implemented by HashMap class is used to replace the value of the specified key only if the key is previously mapped with some value. Syntax: public V replace(K key, V value) Parameters: This method accepts two parameters: key: which is the key of the element whose value has to be replaced. value:
3 min read
HashMap putIfAbsent(key, value) method in Java with Examples
The putIfAbsent(K key, V value) method of HashMap class is used to map the specified key with the specified value, only if no such key exists (or is mapped to null) in this HashMap instance. Syntax: public V putIfAbsent(K key, V value)Parameters: This method accepts two parameters: key: which is the key with which provided value has to be mapped.va
3 min read
HashMap merge(key, value, BiFunction) method in Java with Examples
The merge(Key, Value, BiFunctional) method of the HashMap class is used to combine multiple mapped values for a key using the given mapping function. A bucket is actually an index of the array, that array is called a table in HashMap implementation. So table[0] is referred to as bucket0, table[1] as bucket1, and so on. If the key is not present or
3 min read
HashMap computeIfPresent(key, BiFunction) method in Java with Examples
The computeIfPresent(Key, BiFunction) method of HashMap class which allows you to compute value of a mapping for specified key if key is already associated with a value (or is mapped to null). If mapping function of this method returns null, the mapping is removed. If the remapping function throws an exception, the exception is rethrown, and the ma
2 min read
How to check if a key exists in a HashMap in Java
Given a HashMap and a key in Java, the task is to check if this key exists in the HashMap or not. Examples: Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2 Output: true Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 10 Output: false Using Iterator (Not Efficient): Get the HashMap and the Key Create an iterator to iterate over the
4 min read