EnumSet.range builds a contiguous slice using the enum's declaration order, and complementOf gives every value that a set is missing, both backed by the same compact bit-vector representation as EnumSet.
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
EnumSet<Day> weekdays = EnumSet.range(Day.MON, Day.FRI);
System.out.println("Weekdays: " + weekdays);
EnumSet<Day> weekend = EnumSet.complementOf(weekdays);
System.out.println("Weekend (complement): " + weekend);
Weekdays: [MON, TUE, WED, THU, FRI]
Weekend (complement): [SAT, SUN]
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27