Can a Java 8 stream process an expanding number of elements? -
i had programming exercise find palindrome combinations. example, palindromes in list
[ "abc", "def", "zzb", "cba", "yes", "fed" ]
the answers be
["abc", "cba"], ["cba", "abc"], ["def", "fed"], ["fed", "def"], ["abc", "def", "fed", "cba"]
and on. maybe 8 data set.
i note number of combinations consult depends on n!, meaning list small ten elements means searching on 3 million combos.
to conserve memory wrote functions took initial list , created permutations, each found permutation , processed. @ no time entire bloated list in memory.
i didn't use java 8 streams because presume can't add stream source (in case, 6 elements), , internally system buffer permutations maximum list size overflow memory.
is there stream solution in java exercise? know how create palindromes. seek how specify small list, permutate of combos, , never use more bit of memory end result.
thanks, jerome.
you want use static method of java.util.stream.stream
:
static <t> stream<t> generate(supplier<t> s)
returns infinite sequential unordered stream each element generated provided supplier.
you'll need class returns new combination every invocation of get()
. may challenge write w/o python's yield
:)
Comments
Post a Comment