BAEL-823 Iterating over enum values in Java (#1886)
* Example Code For Evaluation Article This is an example code for the evaluation article on "Different Types of Bean Injection in Spring" * Added unit tests * Minor changes to application context * BAEL-823 Iterating over enum values in Java * Removed code committed for evaluation article
This commit is contained in:
parent
4fd99ca7a5
commit
3b1337598f
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.java.enumiteration;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public enum DaysOfWeekEnum {
|
||||||
|
SUNDAY("off"),
|
||||||
|
MONDAY("working"),
|
||||||
|
TUESDAY("working"),
|
||||||
|
WEDNESDAY("working"),
|
||||||
|
THURSDAY("working"),
|
||||||
|
FRIDAY("working"),
|
||||||
|
SATURDAY("off");
|
||||||
|
|
||||||
|
private String typeOfDay;
|
||||||
|
|
||||||
|
DaysOfWeekEnum(String typeOfDay) {
|
||||||
|
this.typeOfDay = typeOfDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTypeOfDay() {
|
||||||
|
return typeOfDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTypeOfDay(String typeOfDay) {
|
||||||
|
this.typeOfDay = typeOfDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Stream<DaysOfWeekEnum> stream() {
|
||||||
|
return Stream.of(DaysOfWeekEnum.values());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.java.enumiteration;
|
||||||
|
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
public class EnumIterationExamples {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Enum iteration using forEach:");
|
||||||
|
EnumSet.allOf(DaysOfWeekEnum.class).forEach(day -> System.out.println(day));
|
||||||
|
|
||||||
|
System.out.println("Enum iteration using Stream:");
|
||||||
|
DaysOfWeekEnum.stream().filter(d -> d.getTypeOfDay().equals("off")).forEach(System.out::println);
|
||||||
|
|
||||||
|
System.out.println("Enum iteration using for loop:");
|
||||||
|
for (DaysOfWeekEnum day : DaysOfWeekEnum.values()) {
|
||||||
|
System.out.println(day);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue