Home » Java programming language

Java EnumMap containsKey() Method with Example

EnumMap Class containsKey() method: Here, we are going to learn about the containsKey() method of EnumMap Class with its syntax and example.
Submitted by Preeti Jain, on February 10, 2020

EnumMap Class containsKey() method

  • containsKey() method is available in java.util package.
  • containsKey() method is used to check whether this map has values for the given key element (key_ele) of this enum map.
  • containsKey() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • containsKey() method does not throw an exception at the time of checking key mappings.

Syntax:

    public boolean containsKey(Object key_ele);

Parameter(s):

  • Object key_ele – represents the key element (key_ele) whose presence is to be checked.

Return value:

The return type of this method is boolean, it returns true when this enum map have any value for the given key element (key_ele) otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean containsKey(Object key_ele) method of EnumMap

import java.util.*;

public class ContainsKeyOfEnumMap {
    public enum Colors {
        RED,
        BLUE,
        PINK,
        YELLOW
    };

    public static void main(String[] args) {
        // We are creating EnumMap object
        EnumMap < Colors, String > em =
            new EnumMap < Colors, String > (Colors.class);

        // By using put() method is to
        // add the linked values in an EnumMap
        em.put(Colors.RED, "1");
        em.put(Colors.BLUE, "2");
        em.put(Colors.PINK, "3");
        em.put(Colors.YELLOW, "4");

        // Display EnumMap
        System.out.println("EnumMap :" + em);

        // By using containsKey() method isto
        // check whether this EnumMap contains
        // any value for the given key element
        // in an EnumMap
        boolean status = em.containsKey(Colors.PINK);

        // Display status of EnumMap
        System.out.println("em.containsKey(Colors.PINK): " + status);
    }
}

Output

EnumMap :{RED=1, BLUE=2, PINK=3, YELLOW=4}
em.containsKey(Colors.PINK): true


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.