Avoid NoSuchElementException with Stream

前端 未结 3 1058
傲寒
傲寒 2020-12-08 13:09

I have the following Stream:

Stream stream = stream();

T result = stream.filter(t -> {
    double x = getX(t);
    double y = getY(         


        
3条回答
  •  一整个雨季
    2020-12-08 14:07

    You can use Optional.orElse, it's much simpler than checking isPresent:

    T result = stream.filter(t -> {
        double x = getX(t);
        double y = getY(t);
        return (x == tx && y == ty);
    }).findFirst().orElse(null);
    
    return result;
    

提交回复
热议问题