import java.util.EnumSet; enum MyEnum { FOO, BAR, BAZ } public class Example { public static void main(String[] args) { EnumSetmySet = EnumSet.of(MyEnum.FOO, MyEnum.BAR, MyEnum.BAZ); System.out.println("Before removal: " + mySet); mySet.remove(MyEnum.BAR); System.out.println("After removal: " + mySet); } }
Before removal: [FOO, BAR, BAZ] After removal: [FOO, BAZ]In this example, we create an EnumSet called mySet that contains all three values of the MyEnum enum type. We then use the remove() method to remove the MyEnum.BAR value from mySet, and print the contents of the set before and after the removal. Overall, java.util.EnumSet provides a powerful and efficient way to work with sets of enum constants in Java.