JAVA 8 : Lambda Expressions. Tutorial, example

By | May 4, 2014

Lambda Expressions


JAVA 8 introduces a new language : Lambda Expressions

It’s meant to simplify the syntax when one use an anonymous class containing only one method. These interfaces are known as function interfaces.


Use case

Suppose you have a list of Person (age, last-name, first-name) and you’re asked to create a method that filters and then prints that list from different criteria.


Pre java 8 approach

The idea is to create a method that will filter a list of Person depending on specific attributes.
To avoid having one method per export (exportAdults, exportChildren…) we’ll introduce an interface PersonFilter with one method :
boolean filter(Person p) that will return true if the given person is part of the export we want.

Let’s go:

private static void exportSpecificPopulation(String category, Collection<Person> persons, PersonFilter filter) {
  for (Person p : persons) {
    if (filter.filter(p)) {
      System.out.println(category + ": "+p.getLastname() + " "+ p.getFirstname() + " " + p.getAge());

The code to call this method would be:

// Export Adults
exportSpecificPopulation("adults", persons, 
  new PersonFilter {
    public boolean filter(Person person) {
      return p.getAge()>=ADULT_LEGAL_AGE;
// Export Children
exportSpecificPopulation("children", persons, 
  new PersonFilter {
    public boolean filter(Person person) {
      return p.getAge()<ADULT_LEGAL_AGE;

Not that readable huh! and only to get adults and children.

Java 8 approach

In Java 8 we keep the main export method and the interface. But since the interface only has one method Lambda Reflections can be used.

The example in Java 8

Here is how we use the Lambda Expressions in our example (See Lambda Expressions syntax below) :

exportPopulation("adults", persons, p -> p.getAge()>=ADULT_LEGAL_AGE);
exportPopulation("children", persons, p -> p.getAge()<ADULT_LEGAL_AGE);

Note that it’s equivalent to :

exportPopulation("adults", persons, (Person p) -> {return  p.getAge()>=ADULT_LEGAL_AGE;});
exportPopulation("children", persons, (Person p) -> {return  p.getAge()<ADULT_LEGAL_AGE;});

Lambda expression syntax

(from oracle)

A lambda expression consists of the following:

  • A comma-separated list of formal parameters enclosed in parentheses.

    Note: You can omit the data type of the parameters in a lambda expression. In addition, you can omit the parentheses if there is only one parameter.

  • The arrow token, ->
  • A body, which consists of a single expression or a statement block. This example uses the following expression:

    If you specify a single expression, then the Java runtime evaluates the expression and then returns its value.

    A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:

    email -> System.out.println(email)

Note that a lambda expression looks a lot like a method declaration; you can consider lambda expressions as anonymous methods—methods without a name.

Leave a Reply

Your email address will not be published. Required fields are marked *