|
| 1 | +package com.baeldung.enumset; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.EnumSet; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public class EnumSets { |
| 8 | + |
| 9 | + public enum Color { |
| 10 | + RED, YELLOW, GREEN, BLUE, BLACK, WHITE |
| 11 | + } |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + EnumSet<Color> allColors = EnumSet.allOf(Color.class); |
| 15 | + System.out.println(allColors); |
| 16 | + |
| 17 | + EnumSet<Color> noColors = EnumSet.noneOf(Color.class); |
| 18 | + System.out.println(noColors); |
| 19 | + |
| 20 | + EnumSet<Color> blackAndWhite = EnumSet.of(Color.BLACK, Color.WHITE); |
| 21 | + System.out.println(blackAndWhite); |
| 22 | + |
| 23 | + EnumSet<Color> noBlackOrWhite = EnumSet.complementOf(blackAndWhite); |
| 24 | + System.out.println(noBlackOrWhite); |
| 25 | + |
| 26 | + EnumSet<Color> range = EnumSet.range(Color.YELLOW, Color.BLUE); |
| 27 | + System.out.println(range); |
| 28 | + |
| 29 | + EnumSet<Color> blackAndWhiteCopy = EnumSet.copyOf(EnumSet.of(Color.BLACK, Color.WHITE)); |
| 30 | + System.out.println(blackAndWhiteCopy); |
| 31 | + |
| 32 | + List<Color> colorsList = new ArrayList<>(); |
| 33 | + colorsList.add(Color.RED); |
| 34 | + EnumSet<Color> listCopy = EnumSet.copyOf(colorsList); |
| 35 | + System.out.println(listCopy); |
| 36 | + |
| 37 | + EnumSet<Color> set = EnumSet.noneOf(Color.class); |
| 38 | + set.add(Color.RED); |
| 39 | + set.add(Color.YELLOW); |
| 40 | + set.contains(Color.RED); |
| 41 | + set.forEach(System.out::println); |
| 42 | + set.remove(Color.RED); |
| 43 | + } |
| 44 | + |
| 45 | +} |
0 commit comments