Learn the lambda essay on the Java Core Technology volume

Source: Internet
Author: User

Lambda An expression

form: parameter, arrow (-), expression

Parameter type to write, if you can deduce it can not write

Only one argument can omit parentheses

No arguments to write empty brackets ()

* If the branch returns a value, then all branches will return a value, otherwise it is illegal

function interface: interface with only one abstract method (can be marked with @functionalinterface annotations)

For example, you can do this in Javax.swing.Timer.

      Timer T = new timer (+, event-

{

...

});

The second parameter of this constructor requires a ActionListener instance, ActionListener

is an interface that has only one method, so you can use a lambda expression.

Method reference

Object::instancemethod

Class::staticmethod

Class::instancemethod

Such as: Event-System.out.println (event) is equivalent to System.out:: println

Math::p ow equivalent to (x, y)-Math.pow (x, y)

In the third case, the first parameter becomes the target of the method, such as: String::comparetoignorecase equivalent to (x, y), X.comparetoignorecase (y)

There is also the ability to use this and super parameters;

Super:: Instancemethod

Constructor Reference:

X-New int[x] equivalent to Int[]::new

Person x = New person (...) equivalent to Person::new

When there are multiple constructors, the compiler infers from the context

Learn the lambda essay on the Java Core Technology volume

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.