Understand the Lambda in Java

Source: Internet
Author: User

What is a lambda?

Lambda expression is an anonymous function, and lambda expression is named after the lambda calculus in mathematics, directly corresponding to it (lambda abstraction), an anonymous function, That is, a function without a function name. A lambda expression can represent closures (notice differs from the traditional mathematical sense).

Pass a variable to an anonymous function and then manipulate the incoming function. Since there are no functions in Java that are absent from classes, Independent functions are usually composed of an anonymous inner class + one method . A lambda expression replaces a function that has no method name, no access modifier, and an explicit return type declaration.

Implementation resolution

The following will parse the lambda and Java logic.

The most important feature of a function is that it assigns a function (method) to a variable.

In a functional programming environment such as JavaScript

var=function(){  ...}

So that's probably what it looks like in Java.

publicvoiddoSomthing(){  ...}

The Public keyword is superfluous, and the code becomes this way.

voiddoSomthing(){  ...}

This return value type can be deduced from the internal return type, so the return value type is not required.

doSomthing(){  ...}

The method name has been defined before, and the code becomes this way.

var doSomthing = (){  ...}

Because it looks weird. So use-----Define method parameters and principals. () represents the parameter list of the method, and {} internally represents the method body. The single-line method body can omit {}, which is strongly not recommended.

var doSomthing = ()->{  ...}

this is equivalent to a block of code being assigned to a variable , which is a lambda expression.

var is not in Java in this keyword 1.8 version. So what is this variable?

In java1.8, all lambda expressions are themselves implementations of an interface , so var should be an interface.
The following defines an interface

publicinterface MethodInterface {    voiddoSomething();}

An interface function needs to be implemented interface type, we call it "function interface". In order to avoid later people in this interface to add an interface function resulting in its multiple interface functions need to be implemented, into a "non-function interface", we can add a declaration @functionalinterface, so that others will not be added to the inside of the new interface function.

@FunctionalInterfacepublicinterface MethodInterface {    voiddoSomething();}

A functional interface usually has only one method.

We can simply pass lambda as a parameter to the function, whereas the traditional Java must have a clear definition of the interface implementation.
Then the above function becomes

  MethodInterface methodInterface = ()->{    ...  }

So how will we use it?

  methodInterface.doSomthing();

You can call this.

This interface is very versatile and too cumbersome to define, and we have other ways to replace the Methodinterface interface.

Yes, a large number of well-defined interfaces are included in the jdk1.8 java.util.function package.

Interface Name Parameters return value
Consumer<t> T void
Biconsumer<t, u> T,u void
Biconsumer<t, U, r> T,u R
Booleansupplier No Boolean
Doublebinaryoperator Double,double Double

The above enumeration is not entirely.
Of course, you can customize the interface method type as in the above article but need @functionalinterface annotations

Actual combat code

Create Thread interface mode
Interface

package java.lang;@FunctionalInterfacepublicinterface Runnable {    voidrun();}
 Public classlambdatest {Private Static Final intTotal = the;Private StaticAtomicinteger i =NewAtomicinteger (0); Public Static void Main(string[] args)throwsinterruptedexception {Executor Executor = executors.Newcachedthreadpool(); Countdownlatch Countdownlatch =NewCountdownlatch (total); for(intj=0; j<total;j++) {executor.Execute(()->{Add(); Countdownlatch.Countdown();        }); } countdownlatch.await(); System. out.println("The result of the summation is:"+i); }Private Static void Add() {i.Incrementandget(); }}
Summarize

1. A block of code is assigned a value to a variable.
The 1.LAMBDA expression itself is an implementation of an interface.

Understand the Lambda in Java

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.