我有以下Stream
:与流避免NoSuchElementException异常
Stream<T> stream = stream();
T result = stream.filter(t -> {
double x = getX(t);
double y = getY(t);
return (x == tx && y == ty);
}).findFirst().get();
return result;
但是,并不总是这给了我下面的错误结果:
NoSuchElementException: No value present
所以,我怎么可以返回null
如果目前没有价值?
或者直接返回'Optional',这可能比返回null有一些优点。 – Zhedar