306

The new Java 8 stream framework and friends make for some very concise Java code, but I have come across a seemingly-simple situation that is tricky to do concisely.

Consider a List<Thing> things and method Optional<Other> resolve(Thing thing). I want to map the Things to Optional<Other>s and get the first Other.

The obvious solution would be to use things.stream().flatMap(this::resolve).findFirst(), but flatMap requires that you return a stream, and Optional doesn't have a stream() method (or is it a Collection or provide a method to convert it to or view it as a Collection).

The best I can come up with is this:

things.stream()
    .map(this::resolve)
    .filter(Optional::isPresent)
    .map(Optional::get)
    .findFirst();

But that seems awfully long-winded for what seems like a very common case.

Anyone have a better idea?

Lii
  • 11,553
  • 8
  • 64
  • 88
Yona Appletree
  • 8,801
  • 6
  • 35
  • 52
  • After coding a bit with your example, I actually find the explicit version more readable than the one concerning, if it had existed `.flatMap(Optional::toStream)`, with your version you actually see what is going on. – skiwi Mar 29 '14 at 17:48
  • 21
    @skiwi Well, `Optional.stream` exists in JDK 9 now.... – Stuart Marks Feb 25 '15 at 22:30
  • I'm curious where this is documented, and what the process for getting it in was. There are some other methods that really seem like they should exist, and I'm curious where discussion for API changes is taking place. – Yona Appletree Mar 02 '15 at 00:56
  • 3
    https://bugs.openjdk.java.net/browse/JDK-8050820 – Christoffer Hammarström Mar 12 '15 at 13:57
  • 12
    The funny thing is that JDK-8050820 actually refers to this question in its description! – Didier L Jul 02 '15 at 15:29
  • In a pure performance perspective, I think what you did is just fine. I'm not sure how Java 8 implements `flatMap` but this usually introduces a merge which doesn't scale as well as some simple `.filter().map()`. – Crystark Nov 10 '15 at 16:08
  • I hope my answer may help you: https://stackoverflow.com/a/58281000/3477539 – Rostislav V Oct 08 '19 at 06:13

12 Answers12

352

Java 9

Optional.stream has been added to JDK 9. This enables you to do the following, without the need of any helper method:

Optional<Other> result =
    things.stream()
          .map(this::resolve)
          .flatMap(Optional::stream)
          .findFirst();

Java 8

Yes, this was a small hole in the API, in that it's somewhat inconvenient to turn an Optional<T> into a zero-or-one length Stream<T>. You could do this:

Optional<Other> result =
    things.stream()
          .map(this::resolve)
          .flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
          .findFirst();

Having the ternary operator inside the flatMap is a bit cumbersome, though, so it might be better to write a little helper function to do this:

/**
 * Turns an Optional<T> into a Stream<T> of length zero or one depending upon
 * whether a value is present.
 */
static <T> Stream<T> streamopt(Optional<T> opt) {
    if (opt.isPresent())
        return Stream.of(opt.get());
    else
        return Stream.empty();
}

Optional<Other> result =
    things.stream()
          .flatMap(t -> streamopt(resolve(t)))
          .findFirst();

Here, I've inlined the call to resolve() instead of having a separate map() operation, but this is a matter of taste.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
  • 1
    There was a suggested edit on this post showing that the `streamopt` helper can be done much more concise, with the `orElse` method, however I had to deny the edit as it would have been a radical change, you could still apply it to your post though. – skiwi Mar 29 '14 at 11:24
  • 1
    Is there any chance that, if you say it is a small loophole, a method as `Optional.toStream()` (returning either empty or one-element stream) will be added to the JDK? Will it possibly be added in an update to Java 8, or are functional updates never done in an already released version? – skiwi Mar 29 '14 at 11:25
  • 2
    I don't think the api can change until Java 9 now. – assylias Mar 29 '14 at 11:42
  • 1
    @skiwi Re proposed edits, I saw those, and they're more concise but also more obscure. I'd suggest that anyone who wants to can post the alternative in his/her own answer. – Stuart Marks Mar 29 '14 at 16:02
  • 1
    @assylias, skiwi, yes, API changes will have to wait until Java 9. – Stuart Marks Mar 29 '14 at 16:05
  • 1
    @srborlongan I'd encourage you to post your own answer to this question instead of proposing edits to my answer. Yours is a valid comment but it should be under your own name. – Stuart Marks Mar 30 '14 at 18:34
  • I'm going to accept this, though I think that that .filter().map() is probably what I'll use since I try to avoid little easily-forgotten helper methods that others' in the code base won't likely discover. I would love to see a stream() or similar method in Java 9, though. – Yona Appletree Mar 31 '14 at 18:50
  • 6
    @Hypher Thanks. The .filter().map() technique isn't too bad and avoids dependencies on helper methods. 'Twould be nice if there were a more concise way though. I'll investigate getting Optional.stream() added. – Stuart Marks Apr 01 '14 at 01:00
  • 48
    I prefer: `static Stream streamopt(Optional opt) { return opt.map(Stream::of).orElse(Stream.empty()); }` – kubek2k Feb 25 '15 at 22:10
  • 6
    I wish they would just add an `Optional` overload to `Stream#flatMap`... that way you could just write `stream().flatMap(this::resolve)` – flakes Oct 20 '16 at 15:28
  • 4
    @flkes Yeah we've kicked around this idea, but it doesn't seem to add all that much value now that (in JDK 9) there is `Optional.stream()`. – Stuart Marks Oct 20 '16 at 18:00
  • @StuartMarks I suppose it also keeps code more readable, knowing at a glance whether `resolve` returns a `Stream` or an `Optional`. – flakes Oct 20 '16 at 18:40
  • Note if you're stuck with Java 8, you can get the equivalent of Java 9's optional-to-stream method with `optionalFoo.map(Stream::of).orElseGet(Stream::of)` (for use with `flatMap`). Hurts my head a little, but it works. – Gene Dec 10 '19 at 05:39
  • 1
    @kubek2k Better yet, `opt.map(Stream::of).orElseGet(Stream::empty)` — because for some reason the result of `Stream.empty()` isn’t cached, so your code always re-creates a somewhat costly empty stream, even if there’s no need. – Konrad Rudolph Feb 06 '20 at 12:02
71

I'm adding this second answer based on a proposed edit by user srborlongan to my other answer. I think the technique proposed was interesting, but it wasn't really suitable as an edit to my answer. Others agreed and the proposed edit was voted down. (I wasn't one of the voters.) The technique has merit, though. It would have been best if srborlongan had posted his/her own answer. This hasn't happened yet, and I didn't want the technique to be lost in the mists of the StackOverflow rejected edit history, so I decided to surface it as a separate answer myself.

Basically the technique is to use some of the Optional methods in a clever way to avoid having to use a ternary operator (? :) or an if/else statement.

My inline example would be rewritten this way:

Optional<Other> result =
    things.stream()
          .map(this::resolve)
          .flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty))
          .findFirst();

An my example that uses a helper method would be rewritten this way:

/**
 * Turns an Optional<T> into a Stream<T> of length zero or one depending upon
 * whether a value is present.
 */
static <T> Stream<T> streamopt(Optional<T> opt) {
    return opt.map(Stream::of)
              .orElseGet(Stream::empty);
}

Optional<Other> result =
    things.stream()
          .flatMap(t -> streamopt(resolve(t)))
          .findFirst();

COMMENTARY

Let's compare the original vs modified versions directly:

// original
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())

// modified
.flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty))

The original is a straightforward if workmanlike approach: we get an Optional<Other>; if it has a value, we return a stream containing that value, and if it has no value, we return an empty stream. Pretty simple and easy to explain.

The modification is clever and has the advantage that it avoids conditionals. (I know that some people dislike the ternary operator. If misused it can indeed make code hard to understand.) However, sometimes things can be too clever. The modified code also starts off with an Optional<Other>. Then it calls Optional.map which is defined as follows:

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.

The map(Stream::of) call returns an Optional<Stream<Other>>. If a value was present in the input Optional, the returned Optional contains a Stream that contains the single Other result. But if the value was not present, the result is an empty Optional.

Next, the call to orElseGet(Stream::empty) returns a value of type Stream<Other>. If its input value is present, it gets the value, which is the single-element Stream<Other>. Otherwise (if the input value is absent) it returns an empty Stream<Other>. So the result is correct, the same as the original conditional code.

In the comments discussing on my answer, regarding the rejected edit, I had described this technique as "more concise but also more obscure". I stand by this. It took me a while to figure out what it was doing, and it also took me a while to write up the above description of what it was doing. The key subtlety is the transformation from Optional<Other> to Optional<Stream<Other>>. Once you grok this it makes sense, but it wasn't obvious to me.

I'll acknowledge, though, that things that are initially obscure can become idiomatic over time. It might be that this technique ends up being the best way in practice, at least until Optional.stream gets added (if it ever does).

UPDATE: Optional.stream has been added to JDK 9.

Community
  • 1
  • 1
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
19

You cannot do it more concise as you are already doing.

You claim that you do not want .filter(Optional::isPresent) and .map(Optional::get).

This has been resolved by the method @StuartMarks describes, however as a result you now map it to an Optional<T>, so now you need to use .flatMap(this::streamopt) and a get() in the end.

So it still consists of two statements and you can now get exceptions with the new method! Because, what if every optional is empty? Then the findFirst() will return an empty optional and your get() will fail!

So what you have:

things.stream()
    .map(this::resolve)
    .filter(Optional::isPresent)
    .map(Optional::get)
    .findFirst();

is actually the best way to accomplish what you want, and that is you want to save the result as a T, not as an Optional<T>.

I took the liberty of creating a CustomOptional<T> class that wraps the Optional<T> and provides an extra method, flatStream(). Note that you cannot extend Optional<T>:

class CustomOptional<T> {
    private final Optional<T> optional;

    private CustomOptional() {
        this.optional = Optional.empty();
    }

    private CustomOptional(final T value) {
        this.optional = Optional.of(value);
    }

    private CustomOptional(final Optional<T> optional) {
        this.optional = optional;
    }

    public Optional<T> getOptional() {
        return optional;
    }

    public static <T> CustomOptional<T> empty() {
        return new CustomOptional<>();
    }

    public static <T> CustomOptional<T> of(final T value) {
        return new CustomOptional<>(value);
    }

    public static <T> CustomOptional<T> ofNullable(final T value) {
        return (value == null) ? empty() : of(value);
    }

    public T get() {
        return optional.get();
    }

    public boolean isPresent() {
        return optional.isPresent();
    }

    public void ifPresent(final Consumer<? super T> consumer) {
        optional.ifPresent(consumer);
    }

    public CustomOptional<T> filter(final Predicate<? super T> predicate) {
        return new CustomOptional<>(optional.filter(predicate));
    }

    public <U> CustomOptional<U> map(final Function<? super T, ? extends U> mapper) {
        return new CustomOptional<>(optional.map(mapper));
    }

    public <U> CustomOptional<U> flatMap(final Function<? super T, ? extends CustomOptional<U>> mapper) {
        return new CustomOptional<>(optional.flatMap(mapper.andThen(cu -> cu.getOptional())));
    }

    public T orElse(final T other) {
        return optional.orElse(other);
    }

    public T orElseGet(final Supplier<? extends T> other) {
        return optional.orElseGet(other);
    }

    public <X extends Throwable> T orElseThrow(final Supplier<? extends X> exceptionSuppier) throws X {
        return optional.orElseThrow(exceptionSuppier);
    }

    public Stream<T> flatStream() {
        if (!optional.isPresent()) {
            return Stream.empty();
        }
        return Stream.of(get());
    }

    public T getTOrNull() {
        if (!optional.isPresent()) {
            return null;
        }
        return get();
    }

    @Override
    public boolean equals(final Object obj) {
        return optional.equals(obj);
    }

    @Override
    public int hashCode() {
        return optional.hashCode();
    }

    @Override
    public String toString() {
        return optional.toString();
    }
}

You will see that I added flatStream(), as here:

public Stream<T> flatStream() {
    if (!optional.isPresent()) {
        return Stream.empty();
    }
    return Stream.of(get());
}

Used as:

String result = Stream.of("a", "b", "c", "de", "fg", "hij")
        .map(this::resolve)
        .flatMap(CustomOptional::flatStream)
        .findFirst()
        .get();

You still will need to return a Stream<T> here, as you cannot return T, because if !optional.isPresent(), then T == null if you declare it such, but then your .flatMap(CustomOptional::flatStream) would attempt to add null to a stream and that is not possible.

As example:

public T getTOrNull() {
    if (!optional.isPresent()) {
        return null;
    }
    return get();
}

Used as:

String result = Stream.of("a", "b", "c", "de", "fg", "hij")
        .map(this::resolve)
        .map(CustomOptional::getTOrNull)
        .findFirst()
        .get();

Will now throw a NullPointerException inside the stream operations.

Conclusion

The method you used, is actually the best method.

Community
  • 1
  • 1
skiwi
  • 66,971
  • 31
  • 131
  • 216
6

A slightly shorter version using reduce:

things.stream()
  .map(this::resolve)
  .reduce(Optional.empty(), (a, b) -> a.isPresent() ? a : b );

You could also move the reduce function to a static utility method and then it becomes:

  .reduce(Optional.empty(), Util::firstPresent );
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
Andrejs
  • 26,885
  • 12
  • 107
  • 96
6

As my previous answer appeared not to be very popular, I will give this another go.

A short answer:

You are mostly on a right track. The shortest code to get to your desired output I could come up with is this:

things.stream()
      .map(this::resolve)
      .filter(Optional::isPresent)
      .findFirst()
      .flatMap( Function.identity() );

This will fit all your requirements:

  1. It will find first response that resolves to a nonempty Optional<Result>
  2. It calls this::resolve lazily as needed
  3. this::resolve will not be called after first non-empty result
  4. It will return Optional<Result>

Longer answer

The only modification compared to OP initial version was that I removed .map(Optional::get) before call to .findFirst() and added .flatMap(o -> o) as the last call in the chain.

This has a nice effect of getting rid of the double-Optional, whenever stream finds an actual result.

You can't really go any shorter than this in Java.

The alternative snippet of code using the more conventional for loop technique is going to be about same number of lines of code and have more or less same order and number of operations you need to perform:

  1. Calling this.resolve,
  2. filtering based on Optional.isPresent
  3. returning the result and
  4. some way of dealing with negative result (when nothing was found)

Just to prove that my solution works as advertised, I wrote a small test program:

public class StackOverflow {

    public static void main( String... args ) {
        try {
            final int integer = Stream.of( args )
                    .peek( s -> System.out.println( "Looking at " + s ) )
                    .map( StackOverflow::resolve )
                    .filter( Optional::isPresent )
                    .findFirst()
                    .flatMap( o -> o )
                    .orElseThrow( NoSuchElementException::new )
                    .intValue();

            System.out.println( "First integer found is " + integer );
        }
        catch ( NoSuchElementException e ) {
            System.out.println( "No integers provided!" );
        }
    }

    private static Optional<Integer> resolve( String string ) {
        try {
            return Optional.of( Integer.valueOf( string ) );
        }
        catch ( NumberFormatException e )
        {
            System.out.println( '"' + string + '"' + " is not an integer");
            return Optional.empty();
        }
    }

}

(It does have few extra lines for debugging and verifying that only as many calls to resolve as needed...)

Executing this on a command line, I got the following results:

$ java StackOferflow a b 3 c 4
Looking at a
"a" is not an integer
Looking at b
"b" is not an integer
Looking at 3
First integer found is 3
Community
  • 1
  • 1
Roland Tepp
  • 8,301
  • 11
  • 55
  • 73
5

Late to the party, but what about

things.stream()
    .map(this::resolve)
    .filter(Optional::isPresent)
    .findFirst().get();

You can get rid of the last get() if you create a util method to convert optional to stream manually:

things.stream()
    .map(this::resolve)
    .flatMap(Util::optionalToStream)
    .findFirst();

If you return stream right away from your resolve function, you save one more line.

Youssef NAIT
  • 1,362
  • 11
  • 27
Ljubopytnov
  • 51
  • 1
  • 1
5

I'd like to promote factory methods for creating helpers for functional APIs:

Optional<R> result = things.stream()
        .flatMap(streamopt(this::resolve))
        .findFirst();

The factory method:

<T, R> Function<T, Stream<R>> streamopt(Function<T, Optional<R>> f) {
    return f.andThen(Optional::stream); // or the J8 alternative:
    // return t -> f.apply(t).map(Stream::of).orElseGet(Stream::empty);
}

Reasoning:

  • As with method references in general, compared to lambda expressions, you can't accidentaly capture a variable from the accessible scope, like:

    t -> streamopt(resolve(o))

  • It's composable, you can e.g. call Function::andThen on the factory method result:

    streamopt(this::resolve).andThen(...)

    Whereas in the case of a lambda, you'd need to cast it first:

    ((Function<T, Stream<R>>) t -> streamopt(resolve(t))).andThen(...)

charlie
  • 1,478
  • 10
  • 20
4

If you're stuck with Java 8 but have access to Guava 21.0 or newer, you can use Streams.stream to convert an optional into a stream.

Thus, given

import com.google.common.collect.Streams;

you can write

Optional<Other> result =
    things.stream()
        .map(this::resolve)
        .flatMap(Streams::stream)
        .findFirst();
Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
3

If you don't mind to use a third party library you may use Javaslang. It is like Scala, but implemented in Java.

It comes with a complete immutable collection library that is very similar to that known from Scala. These collections replace Java's collections and Java 8's Stream. It also has its own implementation of Option.

import javaslang.collection.Stream;
import javaslang.control.Option;

Stream<Option<String>> options = Stream.of(Option.some("foo"), Option.none(), Option.some("bar"));

// = Stream("foo", "bar")
Stream<String> strings = options.flatMap(o -> o);

Here is a solution for the example of the initial question:

import javaslang.collection.Stream;
import javaslang.control.Option;

public class Test {

    void run() {

        // = Stream(Thing(1), Thing(2), Thing(3))
        Stream<Thing> things = Stream.of(new Thing(1), new Thing(2), new Thing(3));

        // = Some(Other(2))
        Option<Other> others = things.flatMap(this::resolve).headOption();
    }

    Option<Other> resolve(Thing thing) {
        Other other = (thing.i % 2 == 0) ? new Other(i + "") : null;
        return Option.of(other);
    }

}

class Thing {
    final int i;
    Thing(int i) { this.i = i; }
    public String toString() { return "Thing(" + i + ")"; }
}

class Other {
    final String s;
    Other(String s) { this.s = s; }
    public String toString() { return "Other(" + s + ")"; }
}

Disclaimer: I'm the creator of Javaslang.

Daniel Dietrich
  • 2,262
  • 20
  • 25
2

Null is supported by the Stream provided My library abacus-common. Here is code:

Stream.of(things).map(e -> resolve(e).orNull()).skipNull().first();
user_3380739
  • 1
  • 14
  • 14
0

What about that?

private static List<String> extractString(List<Optional<String>> list) {
    List<String> result = new ArrayList<>();
    list.forEach(element -> element.ifPresent(result::add));
    return result;
}

https://stackoverflow.com/a/58281000/3477539

Rostislav V
  • 1,706
  • 1
  • 19
  • 31
-5

Most likely You are doing it wrong.

Java 8 Optional is not meant to be used in this manner. It is usually only reserved for terminal stream operations that may or may not return a value, like find for example.

In your case it might be better to first try to find a cheap way to filter out those items that are resolvable and then get the first item as an optional and resolve it as a last operation. Better yet - instead of filtering, find the first resolvable item and resolve it.

things.filter(Thing::isResolvable)
      .findFirst()
      .flatMap(this::resolve)
      .get();

Rule of thumb is that you should strive to reduce number of items in the stream before you transform them to something else. YMMV of course.

Roland Tepp
  • 8,301
  • 11
  • 55
  • 73
  • 7
    I think the OP's resolve() method returning Optional is a perfectly sensible use of Optional. I can't speak to the OP's problem domain, of course, but it could be that the way to determine whether something is resolvable is to attempt to resolve it. If so, Optional fuses a boolean result of "was this resolvable" with the result of the resolution, if successful, into a single API call. – Stuart Marks Mar 29 '14 at 04:18
  • 2
    Stuart is basically correct. I have a set of search terms in order of desirability, and I'm looking to find the result of the first one that returns anything. So basically `Optional searchFor(Term t)`. That seems to fit the intention of Optional. Also, stream()s should be lazily evaluated, so no extra work resolving terms past the first matching one should occur. – Yona Appletree Mar 31 '14 at 18:43
  • The question is perfectly sensible and using flatMap with Optional is often practiced in other, similar programming languages, such as Scala. – dzs Mar 31 '16 at 07:21