55

I have the following 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;

However, there is not always a result which gives me the following error:

NoSuchElementException: No value present

So how can I return a null if there is no value present?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
clankill3r
  • 9,146
  • 20
  • 70
  • 126

4 Answers4

125

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;
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
33

Stream#findFirst() returns an Optional which exists specifically so that you don't need to operate on null values.

A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.

Otherwise, Optional#get() throws a NoSuchElementException.

If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.

An Optional will never expose its value if it is null.

If you really have to, just check isPresent() and return null yourself.

Stream<T> stream = stream();

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

if (result.isPresent()) 
    return result.get();
return null;
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    Or just go with returning an `Optional`, which may have some advantages over returning null. – Zhedar Jun 06 '15 at 20:53
1

An alternate method for replacing the Optional.get (which more likely than not fails the user's intentions with a NoSuchElementException) is with a more verbose API introduced in JDK10 termed as Optional.orElseThrow(). In author's words -

Optional.get() is an "attractive nuisance" and is too tempting for programmers, leading to frequent errors. People don't expect a getter to throw an exception. A replacement API for Optional.get() with equivalent semantics should be added.

Note :- The underlying implementation of both these APIs is same, yet the latter reads out more clearly that a NoSuchElementException would be thrown by default if the value is not present which inlines to the existing Optional.orElseThrow​(Supplier<? extends X> exceptionSupplier) implementation used by consumers as an explicit alternate.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • More precisely to answer ['Optional.get()' without 'isPresent()' check](https://stackoverflow.com/questions/38725445/optional-get-without-ispresent-check) marked as duplicate of this thread. – Naman Mar 06 '18 at 09:09
0

If you wish to continue using the object and not throw any exception, then

Optional.isPresent(object) is the way to go

Anvita Shukla
  • 409
  • 7
  • 7