Difficulties of recursion with Streams
David Alayachew
davidalayachew at gmail.com
Tue Nov 11 03:36:29 UTC 2025
Hello @core-libs-dev <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20251110/cc86ae0a/attachment-0001.htm>
More information about the core-libs-dev
mailing list