Suppose there is a simple enum called Type defined like this:
enum Type{
X("S1"),
Y("S2");
private String s;
private Type(String s) {
this.s = s;
}
}
Finding the correct enum for given s
is trivially done with static method with for-loop (assume the method is defined inside enum), e.g.:
private static Type find(String val) {
for (Type e : Type.values()) {
if (e.s.equals(val))
return e;
}
throw new IllegalStateException(String.format("Unsupported type %s.", val));
}
I think the functional equivalent of this expressed with Stream API would be something like this:
private static Type find(String val) {
return Arrays.stream(Type.values())
.filter(e -> e.s.equals(val))
.reduce((t1, t2) -> t1)
.orElseThrow(() -> {throw new IllegalStateException(String.format("Unsupported type %s.", val));});
}
How could we write this better and simpler? This code feels coerced and not very clear. The reduce()
especially seems clunky and abused as it doesn't accumulate anything, performs no calculation and always simply returns t1
(provided the filter returns one value - if it doesn't that's clearly a disaster), not to mention t2
is there superfluous and confusing. Yet I couldn't find anything in Stream API that simply somehow returns directly a T
from a Stream<T>
.
Is there a better way?
question from:
https://stackoverflow.com/questions/27807232/finding-enum-value-with-java-8-stream-api 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…