BAEL-2527 Added ArrayList example to EnumIterationExamples

This commit is contained in:
pcoates 2019-02-06 20:04:30 +00:00
parent 46bad01ab9
commit 5ef2a9040a
1 changed files with 27 additions and 2 deletions

View File

@ -1,18 +1,43 @@
package com.baeldung.java.enumiteration; package com.baeldung.java.enumiteration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List;
public class EnumIterationExamples { public class EnumIterationExamples {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Enum iteration using forEach:"); System.out.println("Enum iteration using EnumSet:");
EnumSet.allOf(DaysOfWeekEnum.class).forEach(day -> System.out.println(day)); EnumSet.allOf(DaysOfWeekEnum.class).forEach(day -> System.out.println(day));
System.out.println("Enum iteration using Stream:"); System.out.println("Enum iteration using Stream:");
DaysOfWeekEnum.stream().filter(d -> d.getTypeOfDay().equals("off")).forEach(System.out::println); DaysOfWeekEnum.stream().filter(d -> d.getTypeOfDay().equals("off")).forEach(System.out::println);
System.out.println("Enum iteration using for loop:"); System.out.println("Enum iteration using a for loop:");
for (DaysOfWeekEnum day : DaysOfWeekEnum.values()) { for (DaysOfWeekEnum day : DaysOfWeekEnum.values()) {
System.out.println(day); System.out.println(day);
} }
System.out.println("Enum iteration using Arrays.asList():");
Arrays.asList(DaysOfWeekEnum.values()).forEach(day -> System.out.println(day));
System.out.println("Add Enum values to ArrayList:");
List<DaysOfWeekEnum> days = new ArrayList<>();
days.add(DaysOfWeekEnum.FRIDAY);
days.add(DaysOfWeekEnum.SATURDAY);
days.add(DaysOfWeekEnum.SUNDAY);
for (DaysOfWeekEnum day : days) {
System.out.println(day);
}
System.out.println("Remove SATURDAY from the list:");
days.remove(DaysOfWeekEnum.SATURDAY);
if (!days.contains(DaysOfWeekEnum.SATURDAY)) {
System.out.println("Saturday is no longer in the list");
}
for (DaysOfWeekEnum day : days) {
System.out.println(day);
}
} }
} }