0

I have the following statement:

Function<Stream<Supplier<Collection<? extends User>>>, Stream<User>> userStreamSupplier =
   supStream -> {
    ArrayList<User> list = new ArrayList<>();
    supStream.forEach(sup -> list.addAll(sup.get()));
    return list.stream();
};

Is it possible to convert a loop in the inner lambda to a smart stream.reduce operation (or other aggregation)? I'd tried various approaches but I failed.

Greetings, JG.

1 Answer 1

5

You could use flatMap instead:

supStream -> supStream.flatMap(supp -> supp.get().stream());

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.