Exercise – EnumSet methods
We did not talk about the collection java.util.EnumSet. It is lesser known but very useful class in cases where you need to work with some enum values. Look up its API online and write code that demonstrates the usage of its four methods:
of()complementOf()allOf()range()
Answer
Assuming the enum class looks like the following:
enum Transport { AIRPLANE, BUS, CAR, TRAIN, TRUCK }Then the code that demonstrates the four methods of EnumSet may look like this:
EnumSet<Transport> set1 = EnumSet.allOf(Transport.class); System.out.println(set1); //prints: [AIRPLANE, BUS, CAR, TRAIN, TRUCK] EnumSet<Transport> set2 = EnumSet.range(Transport.BUS, Transport.TRAIN); System.out.println(set2); //prints: [BUS, CAR, TRAIN] EnumSet<Transport> set3 = EnumSet.of(Transport.BUS, Transport.TRUCK); System.out.println(set3); //prints: [BUS, TRUCK] EnumSet<Transport> set4 = EnumSet.complementOf(set3); System.out.println(set4); //prints: [AIRPLANE, CAR, TRAIN...