I understand the logic of your function as follows:
- If there is a SUNDAY or MONDAY at the entrance, return the first one.
- Otherwise, if TUESDAY, return it.
- Otherwise, returns null.
You can implement this logic in Java-8 style by repeating the list twice and using Optional.orElseGet:
myObjList.stream().map(MyObj::getDay).filter(d -> d == Day.SUNDAY || d == Day.MONDAY)
.findFirst()
.orElseGet(() -> myObjList
.stream().map(MyObj::getDay).anyMatch(d -> d == Day.TUESDAY) ? Day.TUESDAY : null);
, .
:
int priority(Day d) {
switch(d) {
case SUNDAY:
case MONDAY:
return 10;
case TUESDAY:
return 5;
default:
return 0;
}
}
Stream.max:
return myObjList.stream().map(MyObj::getDay).max(Comparator.comparingInt(this::priority))
.filter(day -> priority(day) > 0).orElse(null);
, , , : , priority.