java8 new feature lambda expression

jdk8 was officially released in March 2014, and it has been more than 3 years. However, for most programmers, jdk8 is still relatively unfamiliar. I believe that most people use jdk7 in the company. Without further ado, let's learn about the new feature in java8 - lambda expression in this article.

Example of the first lambda: new thread

Before jdk8, we need to manually new an interface and implement the only abstract method in it.

@Test
public  void test(){
new Thread(new Runnable() {
@Override
public void run() {
  System.out.println("hello world...");
}
}).start();
 
}




    Now that we use jdk8, we can have a simpler way of writing:

@Test
public void test2(){
new Thread( 
()->{System.out.println("hello world...");
    System.out.println("haha..");}
).start();
}



The role of lambda expressions seems to be clear: to simplify writing, use expressions instead of anonymous inner classes.

Yes, in fact, for all interfaces with only one abstract method, we can use lambda expressions. We can simply understand that the lambda expression here is an instance object of an implementation of this interface. A function is an object? Yes, it sounds incredible, when in fact many functional programming languages ​​do this, such as JavaScript, which is well known, functions are actually Function objects. Well, we've gone too far, let's go back to java.

So, can the classes/interfaces we write ourselves use lambda expressions? Of course, as long as the interface contains only one abstract method, we can boldly use lambdas. Let's reproduce the above example with our own class/interface.

The first is the MyRunnable interface, which contains only one abstract method run.


package com.jelly.java8.lambda;


public interface MyRunnable {
   
public void run();
}


Next is the MyThread class, which needs to receive an object of the MyRunnable interface type in its constructor.

package com.jelly.java8.lambda;

public interface MyRunnable {
   
public void run();
}


The following client test code:

before jdk8

@Test
public void test1(){
new MyThread(new MyRunnable() {

@Override
public void run() {
System.out.println("hello...");

}
}).start();
   
}

How to write jdk8:

@Test
public void test1(){
new MyThread(()->System.out.println("hello...")).start();
   
}



Yes, that's how lambda expressions work, simple. On the other hand, as long as you see the occasion where lambda can be used in the future, then the interface must also contain only one abstract method.

For example, set sorting needs to pass in the comparator interface. You can manually create a new comparator, and then implement the compare method. Of course, you can also use lambda expressions.

You will find that lambda expressions are syntactic sugar for anonymous interface implementations! ! ! Haha, you're right, there are actually quite a few syntactic sugars in java, such as generics, enumerations, and enhanced for.



Alright, enough talk... um, I seem to have forgotten to introduce the syntax of lambdas.

There is an iconic arrow symbol in the middle of the lambda expression ->

To the left of the arrow is a comma-separated list of formal parameters, enclosed in parentheses. The formal parameter can not specify its java type (the lambda expression can infer itself according to the context).

If there is only one parameter, the parentheses can also be omitted. If there are no formal parameters, leave a pair of parentheses () on the left.

To the right of the arrow can be an expression or a block of java code. If it is a code block, it must be enclosed in a pair of curly braces {}, which can contain multiple java statements, each statement ending with a semicolon.

If the lambda expression function needs to return a value, the return statement needs to be executed at the end of the code block. If it is an expression, it does not need to be enclosed in curly braces {} nor terminated by a semicolon.

It should be noted that if there is only one expression on the right side of the lambda, there is no need to return the expression. The lambda will execute the expression and return the result of the expression evaluation automatically.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325804809&siteId=291194637