Java 8 Combat Chapter III

Source: Internet
Author: User
Tags instance method

Lambda function Features:

    • Anonymous-write less and think more.
    • The function--lambda has an argument list, a function body, a return type, and a list of exceptions that can be thrown.
    • Pass-through--LAMBDA expressions can be passed as arguments to methods or stored in variables.
    • Brief introduction

Lambda implies a return statement (you can use return explicitly).

Lambda has three parts (more examples: http://www.cnblogs.com/Hu-Yan/p/7955550.html)

    • Parameter list
    • Arrow
    • Lambda body

Return is a control flow statement that requires {} to be used effectively in a lambda expression.

"Examplestring" is an expression, not a statement, to make it effective in a lambda expression requires the removal of {}, and the use of explicit return statements.

Functional interface: An interface that defines only an abstract method (even if the interface contains a default method). Note whether the interface inherits other methods.

The lambda function allows you to provide an implementation of an abstract method of a functional interface directly inline, and use the entire expression as an instance of a functional interface.

The signature of the abstract method of the functional interface is basically the signature of the lambda expression. This abstract method is called a function descriptor . The lambda expression is valid if the signature matches.

Common functional interfaces: comparable, Runnable, callable, predicate, Consumer, function ...

You can use the predicate interface when you need to represent a Boolean expression that involves type T.

If you need to access an object of type T and perform some action on it, you can use the consumer interface (abstract method: Accept, object that accepts generics, no return.) )。

If you need to map the input object's information to the output, you can use the function interface, which defines the Apply method-an object that takes a generic t and returns a generic R.

Boxing (boxing) mechanism: the Java type is either a reference type or a primitive type. However, generics can only be bound to reference types. Unboxing (unboxing): Converts the reference type to the corresponding original type.

The boxed value requires more memory, and additional memory searches are required to get the original value of the package.

The name of a functional interface for a specialized input parameter type is added to the original type that corresponds to it. such as: Doublepredicate, Intconsumer ... The function interface also has variants for output parameter types: tointfunction<t>, and so on.

use case

corresponding function interface

Boolean expression

predicate

Create object

supplier

Using an object

consumer

from an object

function/tointfunction

merge two values

intbinaryoperator

Comparison of two objects

Comparator/bifunction/tointbifunction

Any functional interface is not allowed to throw a inspected exception (checked exception). If you need a lambda expression to throw an exception, there are two ways:

    1. Customizes a functional interface and declares a inspected exception.
    2. The lambda expression is in a try/catch block.

The type required by a lambda expression is called the target type . The type checking process is as follows:

    1. Find out the declaration of a method
    2. Requires that it be the second formal parameter of the target type.
    3. The functional interface defines an abstract method
    4. The abstract method describes a function descriptor
    5. Any actual parameters of the method must match this requirement.

Caution: If the lambda function throws an exception, the throws statement declared by the abstract method must match.

The same lambda expression can be associated with different functional interfaces, as long as their abstract method signatures are compatible.

If the body of a lambda is a statement expression, he is compatible with a function descriptor that returns void.

When a lambda has only one type of argument to infer, the parentheses on either side of the parameter name can also be omitted.

The basic idea of a method reference: If a lambda-expressed knowledge "calls this method directly", it is better to call it by name instead of describing how to invoke it.

Use of the method reference: the target reference is placed in the delimiter:: Before the name of the method is placed behind.

There are three main methods of method references:

    1. Object::instancemethod (a reference to an instance method of an existing object, which is thought of as a method of invoking an already existing external object)
    2. Classname::staticmethod (method reference to static method)
    3. Classname::instancemethod (a method reference to an instance method of any type, thought of--refers to a method of an object, and the object itself is a parameter to the lambda.) )

Lambda expression refactoring is equivalent to a method reference:

Lambda

method reference

(arg0, rest), Arg0.instancemethod (rest)

ClassName:: Instancemethod

Lambda

method reference

(args), Classname.staticmethod (args)

ClassName:: Staticmethod

Lambda

method reference

(AGS), Expr.instancemethod (args)

Expr:: Instancemethod

The signature of the method reference must match the context type.

Create a reference to a constructor--classname:: New

Number of constructor arguments

Referencing using constructors

1

SUPPLIER<T>

2

function<t, R>

3

bifunction<t, U, R>

> = 4

Java 8 Combat Chapter III

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.