• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting distinct words Java 8  RSS feed

 
Mike London
Ranch Hand
Posts: 1505
11
  • 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
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mike London
Ranch Hand
Posts: 1505
11
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
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: 7993
143
  • 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: 7993
143
  • 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.
 
Norm Radder
Rancher
Posts: 2240
28
  • 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: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that:
 
Mike London
Ranch Hand
Posts: 1505
11
  • 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: 1505
11
  • 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
Rancher
Posts: 2240
28
  • 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: 7993
143
  • 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: 1505
11
  • 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
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan for the imports.  The code now compiles and executes for me.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!