flatMap() concept

Brian Goetz brian.goetz at oracle.com
Wed Nov 14 10:41:04 PST 2012


That's an awfully optimistic way to phrase the question :)

Not 8.  Not 9.  Future beyond that is hard to predict.

On 11/14/2012 1:33 PM, Olexandr Demura wrote:
> BTW, which java version is expected to have coroutines/generators?
>
> 2012/11/14 Brian Goetz <brian.goetz at oracle.com
> <mailto:brian.goetz at oracle.com>>
>
>     Valid observations, but I think you're focusing on an uncommon case of
>     an uncommon case.
>
>     A key design assumption is that *most* stream operations will operate in
>     "push" mode rather than "pull" mode.  Pull mode (using iterators) only
>     comes into play when you have an operation that may not consume all the
>     input.  Most terminal operations do consume all the input (forEach,
>     into, reduce, groupBy); only a few do not (findFirst, findAny) and
>     therefore can use the more efficient push mode.  Similarly, asking
>     explicitly for an iterator() is an "escape hatch" for when you cannot
>     accomplish your goals with the built-in functionality.  (That's the
>     first level of "uncommon case"; the second level is that you are mapping
>     one element to many.  This happens sometimes, but it happens plenty
>     frequently that one element maps to zero or one.)
>
>     The current design of flatMap is optimized for pushing; it creates no
>     intermediate arrays, collections, iterator, or other bookkeeping
>     overhead for the per-input-element results, and if an element maps to
>     nothing, rather than creating an empty descriptor and iterating it, the
>     implementation simply does nothing.  Much more efficient.  So while
>     there may be a "waste of resources" in some cases, the current design is
>     far more resource-efficient in the common cases.
>
>


More information about the lambda-dev mailing list