Get Started With Lambda Expressions in Java 8

Get Started with Lambda Expressions in Java 8Lambda expressions are the most touted feature of Java 8. They promise to reduce the amount of code you have to write, make your code less error-prone, and make it easier to read. Let’s understand how lambdas manage to do all this using an example.


Filtering a Collection

One of the most common operations on a collection is to filter it based on some criteria. Assuming that you have a collection of movies, you might want to remove the movies that have low ratings. The traditional way to perform this filtering is using an iterator. You loop over all the elements of the collection and remove the ones that you don’t need.

The Movie class used in this example is a simple Java POJO.

Generalize using an Interface

If you want to filter the collection on multiple criteria, you need to write this kind of iterator/loop based code each time. Most of this code is boilerplate and you’d want to abstract it away.  You can create an interface to represent the test you need to execute on each element (it’s generally called a predicate in programming parlance).

Using this abstraction, you can write a general method that will be able to filter any collection given a predicate instance

Now, you can generalize your original code by making use of anonymous inner classes.

Here comes the lambda

Although this code is much more flexible, it isn’t very intuitive or easy to read. There’s a lot of boilerplate code around the single line that is the main concern here.

This is where the lambda expressions feature of Java 8 performs its magic. This same piece of code, when written using lambdas, looks like this.

Without a doubt, this code is much more concise than before. It is quite obvious what is going on here. You want to remove all elements from the collection movies where for element m, the rating is less than 9.0.


But how does it work internally? From the method signature of removeAll(), the compiler knows that the second argument is of type Predicate. Moreover, the Predicate interface has only one method. That method takes a single argument of type T and from the context of the call, the compiler knows that T is bound to type Movie in this case. Using all this information, it can create an anonymous inner class (similar to our own implementation above) on its own.  In a sense, lambda expressions are just “syntactic sugar” to simplify the code.

Update May 30, 2014

Thanks to Mike and Jonathan of the Toronto Java Users Group (and Reddit users jamanifin, neutronbob & nicoulaj) for pointing out that lambdas do not create anonymous inner classes.  Mike looked at the byte code that’s generated when using the built-in java.util.Collection.removeIf method.

With Lambdas:

Without Lambdas:

This also proves that while lambdas may seem like syntactic sugar,  they are definitely much more.  Thanks guys.

Default Methods

What happens if the Predicate interface contains more than one method? The compiler throws up an error saying that Predicate is not a functional interface. A functional interface is one that has only one abstract method. In other words, if you want to use lambda expressions in your code, make sure that your interface has only one abstract method. If you need to have more methods, then use the “Default Methods” feature of Java 8!


4 Responses to “Get Started With Lambda Expressions in Java 8”

  1. Any chance of a link back to my course I mentioned on Reddit?


  2. Nice tutorial. By the way, I have also shared few examples on Java 8 lambda expressions, which complements your post. Your reader may find that useful.


  3. Do you have a link to the reddit converstation you metnioned?