Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

keyset
enummap
java
communitycreator

What is the EnumMap.keySet() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

EnumMap is similar to Map, except that EnumMap only takes the Enum type as key. Additionally, all the keys must be from a single enum type.

For further details on EnumMap, refer here.

In an EnumMap, we can use the keySet method to get all the keys of the EnumMap object as a Set collection.

Any changes made to the associated set are reflected in the map and vice versa. The set allows elements to be removed, and those keys that are removed from the set are also removed from the map.

Syntax

public Set<K> keySet()

Parameters

This method doesn’t take any arguments.

Return value

The keySet method will return a set viewIt returns a class object that implements the set interface, which operates on the original mapping. of all keys in the map.

Code

The example below shows how to use the keySet() method.

import java.util.EnumMap;
import java.util.Set;
class KeySet {
enum Days{
MON, TUE, WED, THUR, FRI, SAT, SUN
};
public static void main( String args[] ) {
EnumMap<Days, String> map = new EnumMap<>(Days.class);
map.put(Days.MON, "chest");
map.put(Days.TUE, "leg");
System.out.println("The map is : " + map);
Set<Days> keys = map.keySet();
System.out.println("The keys are -" + keys );
System.out.println("\nAdding a new entry Days.WED - shoulder" );
map.put(Days.WED, "shoulder");
System.out.println("The keys are -" + keys );
System.out.println("\nDelting the entry of the key Days.WED");
map.remove(Days.WED);
System.out.println("The keys are -" + keys );
}
}
Calling keySet() method

Explanation

In the above code:

  • In line 1 and 2, we import the EnumMap and Set classes.

  • In line 4, we have created an enum for days of the week with the name Days.

  • In line 8, we create an EnumMap object with the name map.

  • In line 9 and 10, we use the put method to add two mappings (Days.MON="chest", Days.TUE="leg") to the map object.

  • In line 14, we get the keys of the map using the keySet() method and store them in the keys variable.

  • In line 18, we add a new entry (Days.WED="shoulder") to the map.

  • In line 19, we print the keys. For the newly added entry, the key - Days.WED is automatically available in the keys variable without the need to call the keySet() method.

  • In line 22, we delete the mapping for the key Days.WED entry of the map. The deleted entry’s key will also be removed from the keys variable.

RELATED TAGS

keyset
enummap
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring