Trying Out Lambda Expressions in the Eclipse IDE

Learn how to make the best of lambdas and virtual extension methods.

Lambda expressions, also called closures, are a short-form replacement for anonymous classes. Lambda expressions simplify the use of interfaces that declare a single abstract method, which are also called functional interfaces. In Java SE 7, a single method interface can be implemented with one of the following options.

  • Create a class that implements the interface.
  • Create an anonymous class.

A lambda expression can be used to implement a functional interface without creating a class or an anonymous class. Lambda expressions can be used only with interfaces that declare a single method.

Lambda expressions are designed to support a multicore processor architecture, which relies on software that provides parallelism, which in turn, improves performance and reduces completion time.

Benefits of lambda expressions include the following:

  • Concise syntax
  • Method references and constructor references
  • Reduced runtime overhead compared to anonymous classes

Prerequisites

To follow along with the examples in this article, download and install the following software:

Syntax of Lambda Expressions

The syntax of a lambda expression is as follows.

(formal parameter list) ->{ expression or statements }

The parameter list is a comma-separated list of formal parameters that match the formal parameters of the single method in a functional interface. Specifying the parameter types is optional; if the parameter types are not specified, the types are inferred from the context.

The parameter list must be enclosed in within parentheses except when a single parameter is specified without the parameter type; a single formal parameter can be specified without parentheses. If a functional interface method does not specify any formal parameters, empty parentheses must be specified.

The parameter list is followed by the-> operator, which is followed by the lambda body, which is a single expression or a statement block. The lambda body has a result that must be one of the following:

  • void, if the functional interface method result isvoid
  • A Java type, primitive type, or reference type that is the same as the return type of the functional interface method

The lambda body result is returned according to one of the following options:

  • If a single expression is used as the lambda body, the expression value is returned.
  • If the method has a return type and the lambda body is not a single expression, the lambda body must return a value using areturn statement.
  • If the functional interface method result isvoid, areturn statement can be provided, but that is not required.

A statement block must be enclosed within curly braces ({}) unless the statement block is a method invocation statement for a method whose result isvoid. The lambda body result must be the same as the result of the single method in the functional interface. For example, if the functional interface method result isvoid, the lambda expression body must not return a value. If the functional interface method has a return type ofString, the lambda expression body must return aString. If the lambda body is a single statement and the method has a return type, the statement must be areturn statement. When a lambda expression is invoked, the code in the lambda body is run.

Functional Interfaces

A lambda expression is used with a functional interface, which is an interface with essentially one abstract method; the interface can contain a method that is also included in theObject class. Some examples of functional interfaces arejava.util.concurrent.Callable—which has a single method,call()—andjava.lang.Runnable—which has a single method,run().

As a comparison, an anonymous class for an interface involves specifying an instance creation expression for the interface and the compiler creating an instance of a class that implements the interface. Unlike an anonymous class, which specifies the interface type (or class type), a lambda expression does not specify the interface type. The functional interface for which a lambda expression is invoked, also called the target type of a lambda expression, is inferred from the context.

Target Type of a Lambda Expression

A lambda expression has an implicit target type associated with it because an interface type is not explicitly specified. In a lambda expression, the target type of a lambda conversion must be a functional interface. The target type is inferred from the context. Therefore, lambda expressions can be used only in contexts in which the target type can be inferred. Such contexts are

  • A variable declaration
  • An assignment
  • A return statement
  • An array initializer
  • Method or constructor arguments
  • A lambda expression body
  • A ternary conditional expression
  • A cast expression

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注