This C# program works (and prints all the permutations of the elements of a list):
I was trying to rewrite this in Java 8 using streams and was surprised to have so much trouble translating the SelectMany. The Streams class does seem to have a good counterpart.
The C# Where function seems similar to the stream::filter function.
In C#, this selectMany function accepts a first argument that is like a java8 filter and a second argument that that allows one to make a new list based on the selection criteria of the first argument. In this code, it concatenates all the selected elements.
I was hoping to demonstrate the Java 8 stream feature for computing all the permutations of a sequence.
Can someone help me translate and help demonstrate Java streams?
I tried this and didn't succeed. I think part of that might be not being clear on what selectMany does. Or trying the algorithm translation directly. I added flags in hope someone will answer who knows.
Siegfried Heintze wrote:I was trying to rewrite this in Java 8 using streams and was surprised to have so much trouble translating the SelectMany. The Streams class does [not] seem to have a good counterpart.
I presume you meant "does not have a good counterpart".
It does have a good counterpart, actually. See Java 8 Docs for Stream::flatMap. There are also `flatMapToDouble`, `flatMapToInt`, `flatMapToLong` (to unbox, clumsily dealing with Java's inability to use generics on primitive types without boxing).
Somebody does seem to have used flatMapXXX(). I think the problem about generics, maybe, goes even deeper, into Java® having retained primitive types. Fortunately I have read proposals to blur the distinction between primitive types and objects, and I think the sooner, the better. I think it likely that others will know more than me on that point.