• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wanted: Help translating C#/LINQ to Java8/Streams  RSS feed

 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks
Siegfried
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The selectMany is like the filter function in the sense that it accepts a predicate as the first argument.

The second argument is also a lambda that allows you to specify how to concatenate them.

I'm struggling with understanding how this is different from a Java8 filter. Does not the return value for both filter and SelectMany contain a concatenated list of elements?

Here is the difference:
The SelectMany returns a IEnumerable<IEnunumerable<t>> which is a list of lists. Remember IEnumerable is the counterpart of Iterable.

So I'm thinking this SelectMany does not exist already in Java 8. Is there an easy way to write this in Java 8?
Thanks
Siegfried
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I managed:
I defined a custom selectMany function:
The concat function looks like this:
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan! I gave you a cow for answering this. I tried and failed.

I got far enough down the rabbit hole to decide I wouldn't use streams for this .
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started it early this afternoon, thinking it shouldn't be too difficult. Was I mistaken! I learned a lot about streams though
 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Thanks! I'm going to discuss this with my java 8 study group!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!