0

Currently, to convert a List<List<Foo>> to a Stream<Foo>, you have to use the following:

Stream<Foo> stream = list.stream().flatMap(fs -> fs.stream());
//or
Stream<Foo> stream = list.stream().flatMap(Collection::stream);

I think this is exactly what the method references were designed for, and it does improve readability quite a bit. Now consider this:

Stream<Bar> stream = list.stream().flatMap(fs -> fs.getBarList().stream());

Having two chained method calls, no method reference is possible, and I've had this happen to me a few times. While it is not a big issue, it seems to drift away from the method-reference brevity.

Having worked with JavaFX 8 a bit, I noticed that a constant of their API's is the convenience methods. Java is a very verbose language, and it seemed to me that simple method overloads were a big selling point for JavaFX.

So my question is, I wonder why there is no convenience method Stream.flatMap(Collection) that could be called like:

Stream<Bar> stream = list.stream().flatMap(Foo::getBarList);

Is this an intentional omission by the folks at Oracle? Or could this cause any confusion?

Note: I'm aware of the "no-opinion-based-questions policy," and I'm not looking for opinions, I'm just wondering if there is a reason that such a method is not implemented.

MikaelF
  • 3,518
  • 4
  • 20
  • 33

1 Answers1

6
  • Because Stream is already a pretty big interface and there's resistance to making it bigger.
  • Because there's also the workaround list.stream().map(Foo::getBarList).flatMap(List::stream).

You can also see the original discussion at http://mail.openjdk.java.net/pipermail/lambda-libs-spec-observers/2013-April/001690.html ; I'm not seeing that option specifically discussed; it may have been discarded already at that point?

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Thank you very much, I appreciate the answer coming from an expert. I hadn't thought of that workaround, I will definitely use it from now on. – MikaelF Mar 01 '17 at 23:45