Sub topic 4
EnumSet and EnumMap
In earlier chapters you declared the Day enum and used it in a switch. Now you will store enum constants in collections. A collection is an object that holds a group of values, like a list or a set. Java has two collections built just for enums: EnumSet and EnumMap.
Why Special Collections?
A set is a collection that holds each value at most once. A map is a collection that links a key to a value, like a word linked to its meaning in a dictionary.
You could use a regular HashSet or HashMap with enum constants, and it would work. But an enum has a small, fixed number of constants, and each one has an ordinal (its position number). Java uses this to store enum collections in a very compact way:
EnumSetstores its members as bits. Each bit is an on/off switch for one constant.EnumMapstores its values in a plain array, using each key's ordinal as the slot number.
This makes both collections fast and small. They also keep their contents in the order the constants are declared, so printing them gives a predictable result.
Using EnumSet
You do not create an EnumSet with new. You call one of its factory methods instead. A factory method is a static method that builds an object for you.
| Method | What it returns |
|---|---|
| `EnumSet.of(a, b, ...)` | A set with exactly the listed constants |
| `EnumSet.range(a, b)` | Every constant from `a` to `b`, both included |
| `EnumSet.allOf(Day.class)` | Every constant of the enum |
| `EnumSet.noneOf(Day.class)` | An empty set you can add to later |
| `EnumSet.complementOf(set)` | Every constant not in `set` |
Using EnumMap
You create an EnumMap with new, and you pass it the enum's class, such as Day.class. That tells the map which enum its keys come from, so it can size its array.
A Worked Example
This program tracks which days are weekend days and how many hours someone works each day.
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public class Main {
public static void main(String[] args) {
EnumSet<Day> weekend = EnumSet.of(Day.SATURDAY, Day.SUNDAY);
EnumSet<Day> weekdays = EnumSet.complementOf(weekend);
EnumSet<Day> midweek = EnumSet.range(Day.TUESDAY, Day.THURSDAY);
System.out.println(weekend); // [SATURDAY, SUNDAY]
System.out.println(weekdays); // [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
System.out.println(midweek); // [TUESDAY, WEDNESDAY, THURSDAY]
System.out.println(weekend.contains(Day.FRIDAY)); // false
EnumMap<Day, Integer> hours = new EnumMap<>(Day.class);
hours.put(Day.TUESDAY, 6);
hours.put(Day.MONDAY, 8);
for (Map.Entry<Day, Integer> entry : hours.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// MONDAY: 8
// TUESDAY: 6
}
}Look at the loop's output. We put TUESDAY in first, but MONDAY prints first. An EnumMap always follows the order of the enum declaration, not the order you added the keys.
Exercises
Exercise
Write one line that creates an EnumSet named workdays holding MONDAY through FRIDAY, using a single factory method.
Show answer
EnumSet<Day> workdays = EnumSet.range(Day.MONDAY, Day.FRIDAY); The range method includes both end points, so all five weekdays are in the set.
Exercise
You run hours.put(Day.SUNDAY, 2); and then hours.put(Day.MONDAY, 8); on an empty EnumMap. In what order does a loop over hours.entrySet() print the keys, and why?
Show answer
MONDAY first, then SUNDAY. An EnumMap keeps its keys in the order the constants are declared in the enum, not the order they were added.