A detailed explanation of Java8 lambda expression and an example _java

Source: Internet
Author: User
Tags add numbers anonymous

First lambda expression

Before the lambda appears, if we need to write a multithreaded may require the following way:

Runnable Runnable = new Runnable () {
      @Override public
      void Run () {
        System.out.println ("Hello Runnable");
      }
    };
    ...
 Thread.Start ();

The above example would be a lot simpler if you were to use a lambda:

Runnable Noargs = ()->system.out.println ("Hello lambda!~");
...
 Thread.Start ();

A lambda expression is an anonymous function that passes the behavior like data, and the expression uses-> to separate the arguments from the body,-> is preceded by the parameter part, which is the main part later.

Other forms of the lambda

binaryoperator<long> add = (x,y)->x+y;
 OR
 binaryoperator<long> add = (Long x,long y)->x+y;
 OR
 binaryoperator<long> add = (Long x,long y)->{
  x+y;
 
    1. Lambda expressions use () to indicate that there are no parameters.
    2. If the lambda expression contains only one argument, omit ().
    3. If the subject of a lambda expression is a block of code, you need to use {}, which is no different from normal Java code blocks, or it can return or throw an exception.
    4. If the type of the parameter can be inferred by the compiler to omit the parameter type, you can, of course, add it.

The above is all the forms of lambda expressions.

So here's the question:

binaryoperator<long> add = (Long x,long y)->x+y; What does that mean?

This line of code does not add numbers, but instead creates a function that calculates the result of the addition of two digits. The type of add is binaryoperator<long>, which is not a sum of two digits, but the code that adds two numbers. The following example shows how to use this variable:

binaryoperator<long> add = (Long x,long y)->x+y;
 Long res = add.apply (3L, 4L);
 System.out.println ("res=" +res);//output: res=7

Function interface

A function interface is an interface that has only one abstract method, and is used as the type of a lambda expression.

such as Runnable is a function interface.

Create a new function interface of our own:

Public interface Addoperator<t,d> {
  long Add (T one,d two);
}

Use:

Addoperator<long,long> Addoperator = (x, y)->x +y;
 SYSTEM.ERR.PRINTLN ("Custom function Listener res=" +addoperator.add (34L, 65L));

Target type

The target type refers to the type of context in which the lambda expression resides. For example, to assign a lambda expression to a local variable, or to pass a method as a parameter, the type of the local variable or method parameter is the target type of the lambda expression.

Final

If we refer to the local variable in the method in the anonymous inner class, this requires that the local variable be final.

We do not need to declare the referenced external local variable as final in the lambda, but the variable can only be assigned once.

In the following example, if we remove the annotation and assign a value to name again, we cannot compile and display an error message: Local variables referenced from a LAMBDA expression must to be final or effectively Final.

A lambda expression refers to a local variable that must be final or factual.

String name = GetUserName ();
Name= "HI";
Button.addactionlistener (Event->system.out.println ("Name=" +name));

Type inference

Type inference for lambda expressions is an extension of the inferred type of target types introduced in Java7.

Type inference in Java7:

list<string> list= new arraylist<> ();

In the example above we did not specify the type of ArrayList, but based on the type of list, we inferred the ArrayList type of the model.

such as: addoperator<long,long> Addoperator = (x, y)->x +y;

Through this article, I hope to help you, learn to understand, this part of knowledge, thank you for your support for this site!

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.