Difficulties of recursion with Streams
Viktor Klang
viktor.klang at oracle.com
Tue Nov 11 15:48:08 UTC 2025
Hi David,
I think it would be easier to offer feedback if you could elaborate
where the point occurs where you feel the need to drop out of Streams
and into imperative code.
It is also worth noting that sometimes imperative code is the right
choice for maintainability, reviewability, performance and/or other reasons.
On 2025-11-11 04:36, David Alayachew wrote:
> Hello @core-libs-dev <mailto:core-libs-dev at openjdk.org>,
>
> When working with streams, I often run into situations where I have to
> "demote" back to imperative code because I am trying to solve a
> problem best solved by recursion.
>
> Consider the common use case of cycling through permutations to find
> all permutations that satisfy some condition. With recursion, the
> answer is incredibly simple -- just grab an element from the set, then
> call the recursive method with a copy of the set minus the grabbed
> element. Once you reach the empty set, you've reached your terminal
> condition.
>
> Use cases like that are not only incredibly common, but usually,
> embarrassingly parallel. The example above of cycling through
> permutations is only a few lines of imperative code, but I struggle to
> imagine how I would do this with Streams.
>
> I guess let me start by asking -- are there any good ways currently to
> accomplish the above permutation example with Streams? And if not,
> should there be?
>
> Thank you for your time and consideration.
> David Alayachew
--
Cheers,
√
Viktor Klang
Software Architect, Java Platform Group
Oracle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20251111/f952d266/attachment.htm>
More information about the core-libs-dev
mailing list