Merge pull request #6292 from pcoates33/BAEL-2527

BAEL-2527 Add section to Java enum iteration article
This commit is contained in:
Loredana Crusoveanu 2019-02-06 23:54:30 +02:00 committed by GitHub
commit e49d46d240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 7 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.variable.scope.examples; package com.baeldung.scope;
public class BracketScopeExample { public class BracketScopeExample {

View File

@ -1,4 +1,4 @@
package org.baeldung.variable.scope.examples; package com.baeldung.scope;
public class ClassScopeExample { public class ClassScopeExample {

View File

@ -1,4 +1,4 @@
package org.baeldung.variable.scope.examples; package com.baeldung.scope;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;

View File

@ -1,4 +1,4 @@
package org.baeldung.variable.scope.examples; package com.baeldung.scope;
public class MethodScopeExample { public class MethodScopeExample {

View File

@ -1,4 +1,4 @@
package org.baeldung.variable.scope.examples; package com.baeldung.scope;
public class NestedScopesExample { public class NestedScopesExample {

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);
}
} }
} }