FlatMap

Kin-man Chung kinman.chung at oracle.com
Fri Oct 12 13:32:20 PDT 2012


The use of the operation flatMap exposes the Block interface.  For 
instance, if one wants to get the list of all orders from all customers, 
one writes

     customers.stream().flatMap((s,c) -> 
c.getOrders().forEach(o->s.apply(o))

In this case, the purpose of the parameter s is for buffering and 
streaming the elements and can really be hidden from the user.  ( BTW, 
if we have "yield", we can write (c->c.getOrders().forEach(o->yield o)), 
but that's for another discussion.)

I admit that the current flatMap is very general and powerful, but I 
think it is more common to flatten elements that are Streamable.  Is it 
possible to add another flavor of flaMap that takes a function that 
returns a Streamable?  If so, the above example can be simplified to

     customers.stream().flatMap(c -> c.getOrders())

which is much more readable.


More information about the lambda-dev mailing list