java.util.EnumSet class works with ranges of enum constants of an enum type.
An EnumSet can contain enum constants only from one enum type.
Suppose you have an enum type called Day.
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
You can work with a range of days using the EnumSet class.
For example, you can get all days between MONDAY and FRIDAY.
The following code demonstrates how to use the EnumSet class to work with the range for enum constants.
import java.util.EnumSet; enum Day {//from w ww.ja v a 2 s . c o m MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; } public class Main { public static void main(String[] args) { // Get all constants of the Day enum EnumSet<Day> allDays = EnumSet.allOf(Day.class); print(allDays, "All days: "); // Get all constants from MONDAY to FRIDAY of the Day enum EnumSet<Day> weekDays = EnumSet.range(Day.MONDAY, Day.FRIDAY); print(weekDays, "Weekdays: "); // Get all constants that are not from MONDAY to FRIDAY of the // Day enum Essentially, we will get days representing weekends EnumSet<Day> weekends = EnumSet.complementOf(weekDays); print(weekends, "Weekends: "); } public static void print(EnumSet<Day> days, String msg) { System.out.print(msg); for (Day d : days) { System.out.print(d + " "); } System.out.println(); } }