java枚举类型filter,使用Java 8 Stream API查找枚举值

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.

Is there a better way?

解决方案

I would use findFirst instead:

return Arrays.stream(Type.values())

.filter(e -> e.s.equals(val))

.findFirst()

.orElseThrow(() -> new IllegalStateException(String.format("Unsupported type %s.", val)));

Though a Map could be better in this case:

enum Type{

X("S1"),

Y("S2");

private static class Holder {

static Map MAP = new HashMap<>();

}

private Type(String s) {

Holder.MAP.put(s, this);

}

public static Type find(String val) {

Type t = Holder.MAP.get(val);

if(t == null) {

throw new IllegalStateException(String.format("Unsupported type %s.", val));

}

return t;

}

}

I learnt this trick from this answer. Basically the class loader initializes the static classes before the enum class, which allows you to fill the Map in the enum constructor itself. Very handy !

Hope it helps ! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值