Java 8--lambda Expressions

Source: Internet
Author: User
Tags compact java se

This article does not introduce Lambda's past life, this is only a detailed understanding of the application of lambda expression. And the difference point with the inner class.

Lambda expression

Lambda expressions are new syntactic sugars introduced in the Java SE 8 release. Consider a function as a method parameter and code as data.

Lambda expression Syntax:

Lambdaparameters-Lambdabody

The lambda expression is divided into three parts:

    • Parameter list
    • Symbol
    • function body

Such as:

() -> {} // 无参,返回结果为空(x) -> System.out.println(x); // 带有一个参数(Thread t) -> { t.start(); }  // 带有一个申明参数(int x, int y) -> x + y; // 带有两个申明参数,一个方法参数;(int x, int y) -> return x +y; // 带有两个申明参数,一个方法参数;(x, y) -> return x +y; // 带有两个申明参数,一个方法参数;

The overall performance of the above-described form, is a new style of Java expression, and ordinary expression is indeed a great difference in style.

The grammatical advantages of this style:

    • The grammatical characteristics determine the concise and compact statement, by reducing the declaration type, return, single statement to the parentheses and so on to become more streamlined;

    • More expressive ability, less redundant code, more attention to the real function statements, more accurate and clear semantics;

Function-Type interface

The lambda expression has already been defined, but how to use it in a compatible object-oriented Java architecture. In object-oriented architecture, everything is an object, how to use the expression as an object, and introduce the function to make the interface: only an abstract method of the interface, representing a single-function contract .

To differentiate between a functional interface and a common interface that contains only an abstract method, you need to use the @functionalinterface annotation callout interface so that the compiler can handle it as a functional interface.

With a functional interface, you can easily use lambda expressions and use Java for functional programming.

Function<String, String> f = (String x) -> x.toUpperCase();f.apply("msg");

A lambda expression can be assigned to a functional interface, where the target type context is involved, and the compiler infers its target type as function based on the context in which the lambda expression resides.

List<String> list = new ArrayList<>();list.forEach((x) -> System.out.println(x));

The lambda expression as a method parameter, in fact the method parameter in the List.foreach method is a functional interface, the compiler infers that the lambda expression type is a functional interface type in foreach as the target type of the lambda expression.

As can be seen from the above:

    • The target type of a lambda expression must be a functional interface, but the functional interface and the lambda expression are two parts, and the functional interface is not part of the lambda expression, but only as its target type;

    • The abstract method parameters of a functional interface must conform to the lambda expression: quantity and number;

    • Function interface Abstract method The return parameter is consistent with the return value of the lambda expression: type;

    • The exception that is thrown in the lambda expression is consistent with the throws on the abstract method of the function interface;

The functional interface makes lambda expressions better to use. Java is object-oriented, and if you introduce lambda on this basis, it is bound to require forward compatibility:

    • If a new type is introduced, it is bound to form two sets of systems with the old API, so that lambda expressions cannot be used in historical versions;

    • If a new type is introduced, the Java class Library needs to maintain two copies of the same functionality: Past historical version/LAMBDA version;

If you use an existing interface to represent:

    • Interface is part of the Java type system

    • The interface naturally has its runtime representation (runtime representation);

For these reasons, selecting an existing interface type as a functional interface and then introducing a lambda expression is the most eclectic way to balance.

Lambda expressions and anonymous inner classes
    1. The simple and compact syntax structure of lambda expressions is unmatched by anonymous inner classes;

    2. Lambda expressions have more explicit semantics-because they focus only on valid code;

    3. The scope of a lambda expression is more friendly than an anonymous inner class (lexical scope/new internal scope): This is represented externally within a lambda expression, and the current inner class instance is represented in an inner class, and the variable name is represented externally in the lambda expression. In the inner class, you want to prevent inheriting from the lexical scope of the superclass ——— Lambda; (Shadow problem)

    4. Lambda is the embodiment of functional programming, the inner class is still at the object-oriented level;

Reference

Deep understanding of Java 8 Lambda (language article--lambda, method reference, target type and default method)

The Java? Language specification

Lambda Expressions

Java 8--lambda Expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.