Java EnumMap - get() Method
The java.util.EnumMap.get() method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null.
Syntax
public V get(Object key)
Here, V is the type of value maintained by the container.
Parameters
key |
Specify the key whose associated value is to be returned. |
Return Value
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
Exception
NA
Example:
In the example below, the java.util.EnumMap.get() method returns the value to which the specified key is mapped in the given EnumMap.
import java.util.*; public class MyClass { //creating an enum public enum weekday{ MON, TUE, WED, THU, FRI } public static void main(String[] args) { //creating EnumMap EnumMap<weekday,Integer> MyMap = new EnumMap<weekday,Integer>(weekday.class); //associate values in MyMap MyMap.put(weekday.MON, 1); MyMap.put(weekday.TUE, 2); MyMap.put(weekday.WED, 3); MyMap.put(weekday.THU, 4); MyMap.put(weekday.FRI, 5); //printing mapped value of specified key System.out.println("MON is mapped to: " + MyMap.get(weekday.MON)); System.out.println("WED is mapped to: " + MyMap.get(weekday.WED)); System.out.println("FRI is mapped to: " + MyMap.get(weekday.FRI)); } }
The output of the above code will be:
MON is mapped to: 1 WED is mapped to: 3 FRI is mapped to: 5
❮ Java.util - EnumMap