Ho il seguente Stream
:Evitare NoSuchElementException con Stream
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;
Tuttavia, non v'è sempre un risultato che mi dà il seguente errore:
NoSuchElementException: No value present
Così come posso restituire un null
se non c'è valore presente?
Oppure basta andare con la restituzione di un 'Optional', che potrebbe avere alcuni vantaggi rispetto alla restituzione di null. – Zhedar