Java.util.EnumMap.get() Method


Advertisements


Description

The java.util.EnumMap.get(Object key) method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Declaration

Following is the declaration for java.util.EnumMap.get() method

public V get(Object key)

Parameters

  • key -- the key whose associated value is to be returned

Return Value

This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Exception

  • NA

Example

The following example shows the usage of java.util.EnumMap.get() method.

package com.tutorialspoint;

import java.util.*;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {

   ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   EnumMap<Numbers, String> map =
   new EnumMap<Numbers, String>(Numbers.class);

   // associate values in map1
   map.put(Numbers.ONE, "1");
   map.put(Numbers.TWO, "2");
   map.put(Numbers.THREE, "3");
   map.put(Numbers.FOUR, "4");


   // print the map
   System.out.println(map);

   // get the value for Numbers.ONE
   String value = map.get(Numbers.ONE);

   // print the result
   System.out.println("Numbers.ONE value:" + value);
   System.out.println("Numbers.FIVE value:" + map.get(Numbers.FIVE));


   }
}

Let us compile and run the above program, this will produce the following result:

{ONE=1, TWO=2, THREE=3, FOUR=4}
Numbers.ONE value:1
Numbers.FIVE value:null

java_util_enummap.htm

Advertisements