OnJava8-Examples/enums/EnumSets.java

37 lines
1.1 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// enums/EnumSets.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
// Operations on EnumSets
2016-07-28 12:48:23 -06:00
// {java enums.EnumSets}
2015-06-15 17:47:35 -07:00
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
*/