• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
Ranch Hand
Posts: 420
  • 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
 
author & internet detective
Posts: 39343
755
Eclipse IDE VI Editor Java
  • 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: 420
  • 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
 
Saloon Keeper
Posts: 10308
217
  • 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
Posts: 39343
755
Eclipse IDE VI Editor Java
  • 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: 10308
217
  • 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: 420
  • 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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Marshal
Posts: 64654
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!