Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Counting distinct words Java 8  RSS feed

 
Ranch Hand
Posts: 1584
13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code:


Is there any way to combine the last two statements into one Java 8 statement?

I don't see how to do the sort in the "collect" statement.

thanks,

- mike
 
Saloon Keeper
Posts: 9257
177
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mike London
Ranch Hand
Posts: 1584
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:



So complicated. Is this really better than just doing a loop I can understand?

I like the streaming API, but when it gets this complicated, it almost seems not worth it.

Appreciate your reply. Excellent.

- mike
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stream API is not meant as a universal tool that can replace every loop in your program. Sometimes it's useful, sometimes it just makes things more complicated. There's nothing wrong with using an old-fashioned for-loop, especially if it makes your code easier to understand.
 
Stephan van Hulst
Saloon Keeper
Posts: 9257
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike London wrote:So complicated. Is this really better than just doing a loop I can understand?


No, it's not. you should always write code you and others understand. My code was just an exact answer to your question.

Note that the solutions ALWAYS requires an intermediate result collection, because you can't determine the top ten words by frequency until you've collected them all. Using collectingAndThen() just hides that intermediate result, but you can still clearly see that two separate stream operations are being performed because stream() is called twice. I think it's better to make this intermediate result explicit:
 
Stephan van Hulst
Saloon Keeper
Posts: 9257
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also like to point out that there's probably no real benefit to making the stream parallel, because it's unlikely that splitting a string can be efficiently parallelized.
 
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you need to compile and execute that code?  I get compiler errors:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Stephan van Hulst
Saloon Keeper
Posts: 9257
177
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that:
 
Mike London
Ranch Hand
Posts: 1584
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I'd also like to point out that there's probably no real benefit to making the stream parallel, because it's unlikely that splitting a string can be efficiently parallelized.



Neither of these seem to work as expected.

The first one isn't in highest-to-lowest order (I'm working on that, but it's not clear how to sort highest to lowest with the SplitAsStream....nothing I've tied so far works), and the second example compiles, but gives a NullPointer error.

Thanks,

- mike
 
Mike London
Ranch Hand
Posts: 1584
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What do you need to compile and execute that code?  I get compiler errors:



Not sure what IDE you're using, but with Intellij, for example, you get the option to import the class if Intellij can't automatically import it.

-- mike
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what IDE you're using


I'm using a fancy editor.
 
Stephan van Hulst
Saloon Keeper
Posts: 9257
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I get for not testing my code.
 
Mike London
Ranch Hand
Posts: 1584
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

what IDE you're using


I'm using a fancy editor.



LOL...apparently not.
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan for the imports.  The code now compiles and executes for me.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!