Collections: EnumSet.range and complementOf build sets from an enum's order

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.

Code
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);
Output
Weekdays: [MON, TUE, WED, THU, FRI]
Weekend (complement): [SAT, SUN]
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms