OnJava8-Examples/enums/EnumSets.java

33 lines
940 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// enums/EnumSets.java
2015-06-15 17:47:35 -07:00
// Operations on EnumSets
package enums;
import java.util.*;
import static enums.AlarmPoints.*;
public class EnumSets {
public static void main(String[] args) {
EnumSet<AlarmPoints> points =
EnumSet.noneOf(AlarmPoints.class); // Empty set
points.add(BATHROOM);
2015-11-03 12:00:44 -08:00
System.out.println(points);
2015-06-15 17:47:35 -07:00
points.addAll(EnumSet.of(STAIR1, STAIR2, KITCHEN));
2015-11-03 12:00:44 -08:00
System.out.println(points);
2015-06-15 17:47:35 -07:00
points = EnumSet.allOf(AlarmPoints.class);
points.removeAll(EnumSet.of(STAIR1, STAIR2, KITCHEN));
2015-11-03 12:00:44 -08:00
System.out.println(points);
2015-06-15 17:47:35 -07:00
points.removeAll(EnumSet.range(OFFICE1, OFFICE4));
2015-11-03 12:00:44 -08:00
System.out.println(points);
2015-06-15 17:47:35 -07:00
points = EnumSet.complementOf(points);
2015-11-03 12:00:44 -08:00
System.out.println(points);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[BATHROOM]
[STAIR1, STAIR2, BATHROOM, KITCHEN]
[LOBBY, OFFICE1, OFFICE2, OFFICE3, OFFICE4, BATHROOM,
UTILITY]
[LOBBY, BATHROOM, UTILITY]
[STAIR1, STAIR2, OFFICE1, OFFICE2, OFFICE3, OFFICE4,
KITCHEN]
2015-09-07 11:44:36 -06:00
*/