EnumMap containsValue(value) method in Java

The Java.util.EnumMap.containsValue(value) method in Java is used to determine whether one or more key of the map is associated with a given value or not. It takes the value as a parameter and returns True if that value is mapped by any of the keys in the EnumMap.

Syntax:

boolean containsValue(Object value)

Parameter: The method accepts one parameter value which refers to the value whose mapping is to be checked by any of the key.

Return Value: It returns true when one or more key is mapped to same value.

Below programs illustrate the containsValue() method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate containsValue() method
import java.util.*;
  
// An enum of gfg ranking is created
public enum rank_countries {
    India,
    United_States,
    China,
    Japan,
    Canada,
    Russia
};
  
class Enum_map {
    public static void main(String[] args)
    {
  
        EnumMap<rank_countries, Integer> mp = new 
        EnumMap<rank_countries,Integer>(rank_countries.class);
  
        // values are associated in mp
        mp.put(rank_countries.India, 72);
        mp.put(rank_countries.United_States, 1083);
        mp.put(rank_countries.China, 4632);
        mp.put(rank_countries.Japan, 6797);
        mp.put(rank_countries.Canada, 1820);
  
        // check if map contains mapping at specified key
        boolean ans = mp.containsValue(72);
  
        // prints the result
        System.out.println("Map contains 72: " + ans);
    }
}

chevron_right


Output:

Map contains 72: true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate containsValue() method
import java.util.*;
  
// An enum of gfg ranking is created
public enum rank_countries {
    India,
    United_States,
    China,
    Japan,
    Canada,
    Russia
};
  
class Enum_map {
    public static void main(String[] args)
    {
  
        EnumMap<rank_countries, Integer> mp = new 
        EnumMap<rank_countries,Integer>(rank_countries.class);
  
        // values are associated in mp
        mp.put(rank_countries.India, 72);
        mp.put(rank_countries.United_States, 1083);
        mp.put(rank_countries.China, 4632);
        mp.put(rank_countries.Japan, 6797);
        mp.put(rank_countries.Canada, 1820);
  
        // check if map contains mapping at specified key
        boolean ans = mp.containsValue(2000);
  
        // prints the result
        System.out.println("Map contains 2000: " + ans);
    }
}

chevron_right


Output:

Map contains 2000: false

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet