Java Utility Library

Java EnumMap - containsValue() Method



The java.util.EnumMap.containsValue() method returns true if this map maps one or more keys to the specified value.

Syntax

public boolean containsValue(Object value)

Parameters

value Specify the value whose presence in this map is to be tested

Return Value

Returns true if this map maps one or more keys to the specified value.

Exception

NA.

Example:

In the example below, the java.util.EnumMap.containsValue() method is used to check whether the EnumMap contains any key which is mapped to the specified value or not.

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    SUN, MON, TUE, WED, THU, FRI, SAT
  }

  public static void main(String[] args) {
    //creating an EnumMap
    EnumMap<weekday,Integer> MyMap = 
        new EnumMap<weekday,Integer>(weekday.class);

    //associate values in the map
    MyMap.put(weekday.MON, 1);
    MyMap.put(weekday.TUE, 2);
    MyMap.put(weekday.WED, 3);
    MyMap.put(weekday.THU, 4);
    MyMap.put(weekday.FRI, 5);

    //printing EnumMap
    System.out.println("MyMap contains: " + MyMap);   

    //check any key mapped to 7
    System.out.print("Does MyMap contain any key mapped to 7? - ");  
    System.out.println(MyMap.containsValue(7));  

    //check any key mapped 5
    System.out.print("Does MyMap contain any key mapped to 5? - ");  
    System.out.println(MyMap.containsValue(5));  
  }
}

The output of the above code will be:

MyMap contains: {MON=1, TUE=2, WED=3, THU=4, FRI=5}
Does MyMap contain any key mapped to 7? - false
Does MyMap contain any key mapped to 5? - true

❮ Java.util - EnumMap