Tuesday 7 April 2015

Java 8 Lambdas in One Line

If you understand this line, or better still can write this code you can pretty much say that you have understood the essence of Java 8 Lambdas. Certainly in as much as they can be used with collections.

I found this in a recent presentation by Peter Lawrey.  (Definitely worth watching the whole presentation when you have a spare hour.)

Anyway the task was to find the 20 most frequent words in a file:

As you can see, with Java 8 this can actually be done in a single (albeit rather long) line. 

If you're not used to lambdas the code might seem a little scary but actually it's pretty declarative and when you get past the logic reads fairly easily.


2 comments:

  1. Compare this to groovy version:
    https://gist.github.com/iNikem/256dfa5de7033eb7cbc4

    Works for java 6 and 7 as well :)

    ReplyDelete
  2. Nice! Your work provides me a good information

    NordVPN Crack

    ReplyDelete