Java 8 Lambda Expressions

1. Overview

In this post, we will discuss the most important feature of Java 8 that is Lambda Expressions. We will learn Lambda Expressions with lots of examples by comparing Java 7 and Java 8 examples.

You can download the source code from my GitHub Repository. This blog can also help to migrate from Java 7 to Java 8.

Lambda Expression facilitates functional programming and simplifies the development a lot.

It provides a clear and concise way to represent one method interface using an expression. It is very useful in the collection library. It helps to iterate, filter and extract data from the collection.

The Lambda expression is used to provide the implementation of an interface which has a functional interface. It saves a lot of code. In case of the lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

Java lambda expression is treated as a function, so the compiler does not create a .class file.

Functional Interface

Lambda expression provides an implementation of the Java 8 Functional Interface. An interface which has only one abstract method is called a functional interface. Java provides an annotation @FunctionalInterface, which is used to declare an interface as a functional interface.

If you have used Runnable, Callable, Comparator, FileFilter, PathMatcher, EventHandler interfaces in your projects then you can replace its implementation with Lambda Expression.

2. Why use Lambda Expression

Lambda Expressions enable you to encapsulate a single unit of behavior and pass it to other code. You can use lambda expressions if you want a certain action performed on each element of a collection, when a process is completed, or when a process encounters an error.

3. Java 8 Lambda Expressions

Java Lambda Expression Syntax

(argument-list) -> {body}

Java lambda expression consists of three components.

Argument-list: It can be empty or non-empty as well.

Arrow-token: It is used to link arguments-list and body of expression.

Body: It contains expressions and statements for the lambda expression.

Let's see an example without lambda expression and with the lambda expression. Here, we are implementing an interface method without using the lambda expression.

One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as a method argument, or code as data.

Summary

Lambda expressions have three parts: a list of parameters, and arrow, and a body: (Object o) -> System.out.println(o);

You can think of lambda expressions as anonymous methods (or functions) as they don't have a name.

A lambda expression can have zero (represented by empty parentheses), one or more parameters.

The type of the parameters can be declared explicitly, or it can be inferred from the context.

If there is a single parameter, the type is inferred and is not mandatory to use parentheses.

If the lambda expression uses as a parameter name which is the same as a variable name of the enclosing context, a compile error is generated.

If the body has one statement, curly brackets are not required, and the value of the expression (if any) is returned.

If the body has more than one statement, curly brackets are required, and if the expression returns a value, it must return with a return statement.

If the lambda expression doesn't return a result, a return statement is optional.

The signature of the abstract method of a functional interface provides the signature of a lambda expression.

In order to use a lambda expression, you first need a functional interface.

However, lambda expressions don't contain the information about which functional interface are implementing.

The type of the expression is deduced from the context in which the lambda is used. This type is called a target type.

The contexts where the target type of a lambda expression can be inferred include an assignment, method or constructor arguments, and a cast expression.

Default methods of a functional interface cannot be accessed from within lambda expressions.