Java Utility Library

Java EnumMap - values() Method



The java.util.EnumMap.values() method returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.

Syntax

public Collection<V> values()

Here, V is the type of value maintained by the container.


Parameters

No parameter is required.

Return Value

Returns a view of the values contained in this enum map.

Exception

NA

Example:

In the example below, the java.util.EnumMap.values() method returns a view of the values contained 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 the content of the map
    System.out.println("MyMap contains: " + MyMap);

    //creating a Collection view
    Collection<Integer> ValueView = MyMap.values();

    //printing the Collection view
    System.out.println("ValueView contains: " + ValueView);
  }
}

The output of the above code will be:

MyMap contains: {MON=1, TUE=2, WED=3, THU=4, FRI=5}
ValueView contains: [1, 2, 3, 4, 5]

❮ Java.util - EnumMap