Home » Java programming language

Java EnumMap get() Method with Example

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

EnumMap Class get() method

  • get() method is available in java.util package.
  • get() method is used to get the value mapped with the given key element (key_ele) otherwise it returns null when no value associated with the given key element (key_ele).
  • get() 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.
  • get() method does not throw an exception at the time of returning element mapped to the given key.

Syntax:

    public Value get(Object key_ele);

Parameter(s):

  • Object key_ele – represents the key element of this enum map.

Return value:

The return type of this method is Value, it returns the mapped value of the given key element (key_ele) when exists else it returns null.

Example:

// Java program to demonstrate the example 
// of Value get(Object key_ele) method of 
// EnumMap

import java.util.*;

public class GetOfEnumMap {
    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)
        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) :" + em);

        // By using get(key_ele) method isto
        // return the value of the given key
        // element (key_ele) when exists otherwise
        // it returns null

        String val_ele = em.get(Colors.BLUE);

        // Display val_ele of the given key 
        // element in an EnumMap
        System.out.println("em.get(Colors.BLUE): " + val_ele);
    }
}

Output

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


Comments and Discussions!

Load comments ↻





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