Idea Analysis: You can specify a type for EnumSet, which is the enum class defined in the same package. Use the add() method of the EnumSet class to add elements, use the remove() method of the EnumSet class to delete elements, use the complementOf() method of the EnumSet class to get all the objects, and use the range() method of the EnumSet class to get elements of the specified range.
The code is as follows:
The code copy is as follows:
package cn.edu.xidian.crytoll;
public enum Weeks {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURADAY, SUNDAY
}
EnumSetTest.java:
The code copy is as follows:
package cn.edu.xidian.crytoll;
import static cn.edu.xidian.crytoll.Weeks.MONDAY;
import static cn.edu.xidian.crytoll.Weeks.THURSDAY;
import java.util.EnumSet;
public class EnumSetTest {
public static void main(String[] args) {
EnumSet<Weeks> week = EnumSet.noneOf(Weeks.class);
week.add(MONDAY);
System.out.println("Element in EnumSet:" + week);
week.remove(MONDAY);
System.out.println("Element in EnumSet:" + week);
week.addAll(EnumSet.complementOf(week));
System.out.println("Element in EnumSet:" + week);
week.removeAll(EnumSet.range(MONDAY, THURSDAY));
System.out.println("Element in EnumSet:" + week);
}
}
Run the program to see the results.