What Are Lambda Expressions?

Lambda expressions add a new ability to the Java language: to pass a function as an argument to another method. Before lambda expressions you could do something similar using anonymous inner classes and functional interfaces, but the result code became less legible and clear:

Lambda expressions let you create single-method classes in a more compact way. The syntax is always the following:

  • parameter-list is a comma-separated list of parameters. You don’t need to specify the type of the parameters (the compiler can determine it by looking at the functional interface method signature).
  • body can be an expression (it’s evaluated as any other expression and its value is returned) or a statement block (it’s evaluated like a method body).

The following are valid lambda expressions examples:

Functional Interfaces

Now that you know how to write basic lambda expressions, the next question you may ask is:

How do I create a method that takes a lambda expression as a parameter?

The answer is functional interfaces. A functional interface (known as a Single Abstract Method type before Java 8) is an interface with only one method. Any method that takes a functional interface as a parameter can be invoked with a lambda expression as an argument for that parameter.
The lambda expression must match the signature of the only functional interface method. If the method does not take any parameter then the parameter list of the lambda expression must be empty: (). If the method does not return anything then the lambda expression body must not return anything.

Predicates

You can create your own functional interfaces to use with lambda expressions, but Java already provide some that cover most use cases and therefore eliminates the need for every programmer to write the same things again and again. Predicate is one of these interfaces provided in the package java.util.function. There are some other useful interfaces in that package (e.g. Function and Consumer) but you don’t need to know about them for the exam.

Predicates are useful whenever you want to invoke a method passing a lambda expression that returns a boolean value:

In the previous example we use the lambda expression to easily filter the list of Device objects that the printReport method prints out to the standard output. Notice that since Predicate is a generic interface, when declaring it you must indicate the actual type of the interface (Predicate<Device> in this case).


Leave a Reply