Skip to content

Conversation

@hoeggi
Copy link

@hoeggi hoeggi commented Dec 16, 2016

Let's you convert an Optional to a Stream. Useful if you have a @Nullable list.

Instead of this

    private static <T> Stream<T> stream(@Nullable T object) {
        return Optional.of(object).map(value -> {
            if (value instanceof Iterable) {
                return Stream.stream((Iterable<T>) value);
            } else {
                return Stream.of(value);
            }
        }).or(Stream.of());
    }

you get this

    private static <T> Stream<T> stream(@Nullable T object) {
        return Optional.of(object).stream();
    }

@artemohanjanyan
Copy link
Contributor

artemohanjanyan commented Dec 16, 2016

Looks a bit confusing, single method with different logic for different types. I'd prefer writing optional.stream().flatten() explicitly.
Also, no tests.

@hoeggi
Copy link
Author

hoeggi commented Dec 16, 2016

Ok, thx for the feedback. Just to define what flatten does.
If you call flatten on a Stream<Iterable<T>> you get a Stream<T>
where a Stream<Value> from something like Stream.of(T value) keeps the same structure?

@artemohanjanyan
Copy link
Contributor

It's .flatMap(x -> x) :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants