The Java.util.EnumMap.containsKey(key) method is used to check whether a specified key mentioned in the parameter is present in this map or not.
Syntax:
boolean containsKey(Object key)
Parameter: The method accepts one parameter key which refer to the key to be verified.
Return Value: The method returns true if the key is present in the EnumMap otherwise it returns false.
Below programs illustrate the containsKey() method:
Program 1:
// Java program to demonstrate containsKey() method import java.util.*; // An enum of gfg visitors is created public enum gfg_visitors { India, United_States, China, Japan, Canada }; class Enum_map { public static void main(String[] args) { EnumMap<gfg_visitors, String> mp = new EnumMap<gfg_visitors, String>(gfg_visitors.class); // values are associated in mp mp.put(gfg_visitors.India, "61.4%"); mp.put(gfg_visitors.United_States, "18.4%"); mp.put(gfg_visitors.China, "2.5%"); mp.put(gfg_visitors.Japan, "1.1%"); mp.put(gfg_visitors.Canada, "1.1%"); // Check if map contains gfg visitor from United_States boolean ans = mp.containsKey(gfg_visitors.United_States); // Prints the result System.out.println("gfg_visitors from United States: " + ans); } } |
gfg_visitors from United States: true
Program 2:
// Java program to demonstrate containsKey() method import java.util.*; // An enum of gfg visitors is created public enum gfg_visitors { India, United_States, China, Japan, Canada, Russia }; class Enum_map { public static void main(String[] args) { EnumMap<gfg_visitors, String> mp = new EnumMap<gfg_visitors, String>(gfg_visitors.class); // values are associated in mp mp.put(gfg_visitors.India, "61.4%"); mp.put(gfg_visitors.United_States, "18.4%"); mp.put(gfg_visitors.China, "2.5%"); mp.put(gfg_visitors.Japan, "1.1%"); mp.put(gfg_visitors.Canada, "1.1%"); // check if map contains gfg visitor from Russia boolean ans = mp.containsKey(gfg_visitors.Russia); // prints the result System.out.println("gfg_visitors from Russia: " + ans); } } |
gfg_visitors from Russia: false
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.TreeMap.containskey() and containsValue() in Java
- HashMap containsKey() Method in Java
- LinkedHashMap containsKey() Method in Java
- Hashtable containsKey() Method in Java
- TreeMap containsKey() Method in Java
- WeakHashMap containsKey() Method in Java
- IdentityHashMap containsKey() Method in Java
- ConcurrentHashMap containsKey() Method in Java
- AbstractMap containsKey() Method in Java with Examples
- Map containsKey() method in Java with Examples
- ConcurrentSkipListMap containsKey() method in Java with Examples
- Properties containsKey(value) method in Java with Examples
- SimpleBindings containsKey() method in Java with Examples
- EnumMap remove() Method in Java
- EnumMap size() Method in Java
- EnumMap equals() Method in Java with Examples
- EnumMap clear() Method in Java with Examples
- EnumMap entrySet() Method in Java
- EnumMap clone() Method in Java
- EnumMap containsValue(value) method in Java
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

