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.
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 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.