Java Utility Library

Java EnumMap - entrySet() Method



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

Syntax

public Set<Map.Entry<K,V>> entrySet()

Here, K and V are the type of key and value respectively maintained by the container.


Parameters

No parameter is required.

Return Value

Returns a set view of the mappings contained in this enum map.

Exception

NA

Example:

In the example below, the java.util.EnumMap.entrySet() method returns a view of the mappings contained in the given EnumMap.

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    SUN, MON, TUE, WED, THU, FRI, SAT
  }

  public static void main(String[] args) {
    //creating an EnumMap
    EnumMap<weekday,Integer> MyMap = 
        new EnumMap<weekday,Integer>(weekday.class);

    //associate values in the map
    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 EnumMap
    System.out.println("MyMap contains: " + MyMap);

    //creating a set view of mapping of the EnumMap
    Set SetView = MyMap.entrySet();

    //printing the set view of mapping
    System.out.println("SetView contains: " + SetView);
  }
}

The output of the above code will be:

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

❮ Java.util - EnumMap