Java Map containsKey() containsValue() Methods Tutorial

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

What is Java Map containsKey() Method?

The Java Map containsKey() method is used to see if a Map object has a specific key in its entries or not.

Java containsKey() Method Syntax:

boolean containsKey(Object key)

containsKey() Method Parameters:

The method takes one argument and that is the key we want to search the target map object for to see if it has the key or not.

containsKey() Method Return Value:

The return value of this method is of type Boolean and will be true if the target map object had the specified key (the argument) and false otherwise.

containsKey() Method Exceptions:

The method might throw two exceptions:

  • ClassCastException: We get this exception if the argument is of incompatible type compared to the keys in the target map object.
  • NullPointerException: We get this exception if the argument is null and the target map object does not permit null keys.

Example: using Map containsKey() 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","Doe");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        System.out.println(map.containsKey("Omid"));
        System.out.println(map.containsKey("AAA"));
    }
}

Output:

true

false

What is Java Map containsValue() Method?

The Java Map containsValue() method is used to check a Map object and see if it has one or more keys that are mapped with the specified value (the argument of this method).

Java containsValue() Method Syntax:

boolean containsValue(Object value)

containsValue() Method Parameters:

The method takes one argument and that is the value we want to check the target Map object for to see if one or more keys have this value mapped to them.

containsValue() Method Return Value:

The return value of this method is of type Boolean and will be true if there was at least one key with the specified value.

containsValue() Method Exceptions:

The method might throw two exceptions:

  • ClassCastException: We get this exception if the argument is of incompatible type compared to the values in the target map object.
  • NullPointerException: We get this exception if the argument is null and the target map object does not permit null values.

Example: using Map containsValue() 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.containsValue("Obama"));
        System.out.println(map.containsValue("AAA"));
    }
}

Output:

true

false
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies