Java Map get() getOrDefault() Methods Tutorial

In this section we will learn what the Map get() and getOrDefault() methods are and how to use them in Java.

What is Java Map get() Method?

The Java Map get() method is used to get the value associated with a key in a map object.

Basically, this method takes one argument and that is the key we want to search for its value in Map.

Java get() Method Syntax:

V get(Object key)

get() Method Parameters:

The method takes one argument and that is the key that we want to look for its value in the target map object.

get() Method Return Value:

The return value of this method is the value associated with the specified key (the argument of the method).

Note: if the target map object didn’t have the declared key, then the return value of this method becomes null.

get() Method Exceptions:

ClassCastException: we get this exception if the first argument (the key) is of incompatible data type compared to the keys in the target map object.

NullPointerException: we get this exception if the first argument (the key) is null and the target map object doesn’t permit a null value.

Example: using Map get() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Dehghan");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        System.out.println(map.get("Omid"));
        System.out.println(map.get("Richard"));
    }
}

Output:

Dehghan

null

What is Java Map getOrDefault() Method?

The Java Map getOrDefault() method is used to find the value associated with a key that we put as the argument of this method and returns that as a result.

Note: however, the difference between this method and the get() method is that we can set a second argument for this method and that will return if the specified key for this method (its first argument) was not in the target Map object.

Basically, using this method we’re saying, check the target map object for a specific key and if you found one, then return its value; otherwise, if there wasn’t such key, then return the default value (that we set as the second argument of the method).

Java getOrDefault() Method Syntax:

default V getOrDefault(Object key, V defaultValue)

getOrDefault() Method Parameters:

The method takes two arguments:

  • The first argument is the key we want to look for in a map object.
  • The second argument is the default value that we want it to be returned only if the target key (the first argument) wasn’t in the target map object.

getOrDefault() Method Return Value:

The return value of this method is the value associated with the first argument of the method or the value we’ve set as the second argument in this method.

getOrDefault() Method Exceptions:

ClassCastException: we get this exception if the first argument (the key) is of incompatible data type compared to the keys in the target map object.

NullPointerException: we get this exception if the first argument (the key) is null and the target map object doesn’t permit a null value.

Example: using Map getOrDefault() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Dehghan");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        System.out.println(map.getOrDefault("Omid","Doe"));
        System.out.println(map.getOrDefault("Richard","Doe"));
    }
}

Output:

Dehghan

Doe
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies