Java.util.EnumMap.containsValue() Method


Advertisements


Description

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

Declaration

Following is the declaration for java.util.EnumMap.containsValue() method

public boolean containsValue(Object value)

Parameters

  • value -- the value whose presence in this map is to be tested.

Return Value

This method returns true if this map maps one or more keys to this value.

Exception

  • NA

Example

The following example shows the usage of java.util.EnumMap.containsValue() method.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {

   ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   EnumMap<Numbers, String> map =
   new EnumMap<Numbers, String>(Numbers.class);

   // associate values in map
   map.put(Numbers.ONE, "1");
   map.put(Numbers.TWO, "2");
   map.put(Numbers.THREE, "3");
   map.put(Numbers.FOUR, "4");

   // print the whole map
   System.out.println(map);

   // check if map contains a mapping at specified key
   boolean contains = map.containsValue("1");

   // print the result
   System.out.println("Map contains '1':" + contains);

   // check if map contains a mapping at another specified key
   contains = map.containsValue("5");

   // print the result
   System.out.println("Map contains '5':" + contains);

   }
}

Let us compile and run the above program, this will produce the following result:

{ONE=1, TWO=2, THREE=3, FOUR=4}
Map contains '1':true
Map contains '5':false

java_util_enummap.htm

Advertisements