0

I have stream of optionals. I would like to return true if any of elemeents of the stream is not present and false if all elements are present.

The code:

return Stream.of(a.getBestArrivalTime(),
        a.getBestDepartureTime(),
        a.getScheduledArrivalTime(),
        a.getScheduledDepartureTime())
        .anyMatch(Objects::isNull);

It checks whether elements are null, but it is wrong because it does not work on optionals variables. I think I need to use Optional::isPresent, but I could not use it because Stream.of() is a static method. The fields in the stream are just chosen fields from the object.

Naman
  • 27,789
  • 26
  • 218
  • 353
pqa1222
  • 75
  • 1
  • 8
  • 1
    "I think I need to use Optional::isPresent" - well, did you try it? Works for me. – Smutje Mar 19 '20 at 12:27
  • what about `anyMatch(o -> !o.isPresent())`? – Lino Mar 19 '20 at 12:27
  • Looks like you cannot share your original code. So, please create a short, minimal reproducible example which mimics your code - https://stackoverflow.com/help/minimal-reproducible-example – MasterJoe Mar 19 '20 at 16:21

2 Answers2

2

You can write the code as you expect such as:

return Stream.of(a.getBestArrivalTime(), a.getBestDepartureTime(),
                 a.getScheduledArrivalTime(), a.getScheduledDepartureTime())
             .anyMatch(o -> !o.isPresent());//(Java-11) anyMatch(Optional::isEmpty)
Naman
  • 27,789
  • 26
  • 218
  • 353
2

What's wrong with

return !Stream.of(a.getBestArrivalTime(),
    a.getBestDepartureTime(),
    a.getScheduledArrivalTime(),
    a.getScheduledDepartureTime())
    .allMatch(Optional::isPresent);

?

Edit: Proof of my comment about your wrong assumption of "non-static method cannot be referenced from static context.":

import java.util.Optional;
import java.util.stream.Stream;

public class Application {

    public static void main (String[] args) {
        final boolean allMatch = !Stream.of(Optional.empty(), Optional.empty())
            .allMatch(Optional::isPresent);
        System.out.println(allMatch);
    }
}
Smutje
  • 17,733
  • 4
  • 24
  • 41
  • It needs to encounter all elements of the Stream. Practically fine in this case, but in general streams are unbounded. – daniu Mar 19 '20 at 12:36
  • 2
    "non-static method cannot be referenced from static context." - has nothing to do with this, sorry – Smutje Mar 19 '20 at 12:41
  • 1
    Maybe simply add a minimal reproducible example and not only snippets of code, then we can help without speculating. – Smutje Mar 19 '20 at 12:50
  • 1
    @pqa1222 you should be more specific about "it stops working". I'm quiet sure, one of the elements you put into the stream is *not* an `Optional`. As said [in this answer](https://stackoverflow.com/a/40174196/2711488), the error message “Non-static method cannot be referred from a static context.” appears almost always when generic types do not match, especially in the context of method references. `javac` just #?@X when is should report the actual error (Eclipse isn't much better, it just produces different, but still misleading error messages). – Holger Mar 19 '20 at 14:01
  • 1
    @daniu all match operations of the Stream API are lazy. `allMatch` stops when it encounters the first non-matching element. – Holger Mar 19 '20 at 14:03
  • 1
    @pqa1222 you didn't include an exact error message yet. As said by Smutje, you should include more code, a reproducible example in the best case, in your question. Add the exact error message and try with command line `javac` to see whether you get a different error message. – Holger Mar 19 '20 at 15:08