Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cactoos flatMap analogy

Tags:

java

cactoos

Is there flatMap analogy in Cactoos library? I need exactly what flatMap can, but without streams:

The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

E.g. if I have some values in list, and each value has children items, and I want to get all items from each value, I can use flatMap:

List<Value> values = someValues();
List<Item> items = values.stream()
  .flatMap(val -> val.items().stream()) // val.items() returns List<Item>
  .collect(Collectors.toList());

How to do the same thing using Cactoos instead of streams API?

like image 425
Kirill Avatar asked Jun 20 '26 05:06

Kirill


1 Answers

You can use Joined, it is the equivalent of flattening an Iterable.

For example, you would write:

new Joined<>(new Mapped<>(val -> val.items(), someValues()));
like image 50
Victor Noël Avatar answered Jun 21 '26 18:06

Victor Noël