3

Is this possible (pseudo-Java):

someList = [
  [1,2],
  [3,4]
];
Stream<List<X>> a = someList.stream();
Stream<X> b = a.whatever(...);
assert b.collect(list).equals([1,2,3,4]);

Put more generally, is there an operation on a stream that can increase the number of elements in the stream (rather than decrease, as filter does)?

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

1 Answers1

3

Yes. flatMap does that.

Stream<X> b = someList.stream().flatMap(l -> l.stream());

Assuming that someList is a List<List<X>>, flatMap would flatten all the elements of the internal lists into a single Stream.

Eran
  • 387,369
  • 54
  • 702
  • 768