Lambda expressions in JDk1.8

1. Introduce an example

Let’s write an example of multithreading as follows: implement the Runable interface

package cn.lyn4ever.lambda; Public class TestMain1 {public static void main(String[] args) {MyThread myThread1 = new MyThread(); Thread t1 = new Thread(myThread1); t1.start(); MyThread myThread2 = new MyThread(); Thread t2 = new Thread(myThread2); t2.start(); */ class MyThread implements Runnable {@override public void run() {MyThread implements Runnable (); System.out.println(" This is a multi-thread :" + thread.currentThread ().getid ()); }}Copy the code

Of course, there is no appointment problem. If it’s too much trouble to write your own outer class, write an anonymous inner class

package cn.lyn4ever.lambda; public class TestMain2 { public static void main(String[] args) { new Thread(new Runnable() { @Override public void Run () {system.out.println (" This is a Thread :"+ thread.currentThread ().getid ()); } }).start(); }}Copy the code

That’s not easy enough, so lambda expressions.

New Thread(()->{system.out.println (" + thread.currentThread ().getid ()); }).start();Copy the code

Yes, that’s all. Look at a result

2. Functional interfaces

A functional interface is an interface that has only one abstract method (including static and default methods, which is a feature of JDK1.8). To customize a functional interface, it is easy to add annotations and the compiler will check the code for you

@functionalinterface public interface MyFunction {/** * define a method to getUpperCase letters * @return */ String getUpperCase(String letter); }Copy the code

And then simply apply our interface

Public static void main(String[] args) {MyFunction MyFunction = new MyFunction() {@override public String GetUpperCase (String letter) {return letter.trim().toupperCase (); }}; Println (myFunction.getupperCase (" This is MyFunctoinTest")); // Then call system.out.println (myFunction.getupperCase ("this is MyFunctoinTest")); //lambda expression MyFunction myFunction1 = (String letter)->{return letter.trim().toupperCase (); }; // Then call the method system.out.println (myfunction1. getUpperCase("this is MyFunctoinTest")); }Copy the code

As you can see, the MyFunction interface we defined is useless because the implementation is written in an anonymous inner class.

java.lang.Runable
java.lang.Comparable
java.lang.Comparator
java.lang.FileFilter
...Copy the code

The java.util.function package also defines a number of functional functional interfaces that you can use without having to create your own. Such as the following

1. Java.util.Predicate takes an object T and returns a Boolean result
Predicate<Integer> Predicate = (Integer param) -> {if (null! = param && param == 0) return true; return false; }; // Call the test method of the predicate system.out.println (predicate. Test (12));Copy the code

You can look at the source code for Predicate

The result of this code must be false, so don’t look at it. This interface will do just fine if we want to receive an object T, return a Boolean result, and so on, without having to define a separate method, just use this class. There are many more, and I’m not going to give you any examples, but let’s list what they do

Java.util.function.Com sumer < T > T incoming object, simply operation, no result (as you all know, class as a parameter is the address) < R > Java. Util. The function. The Funtion of < T > T incoming object, Return object R < T > Java. Util. Function. : Supplier don't accept parameters, < T > T target Java. Util. The function. The UnaryOperator < T > T receive parameters object, return a result object T < T > Java. Util. The function. The BinaryOperator < T > T, receiving two T object, Return a T object (here's an example)Copy the code
public static void main(String[] args) { BinaryOperator<Integer> binaryOperator = (Integer i, Integer j) -> {return I > j? i : j; }; System. The out. Println (binaryOperator. Apply (12, 13)); / / 13}Copy the code

Basic syntax of Lambda expressions

1. Basic grammar

  • 0. Basic Syntax:
The {} and reutrn keywords can be omitted if only one statement is used to implement the method in the expression. Otherwise, they must be written (reutrn is required if there is a return value, but not if there is none) * 3. If the method in the interface has parameters, the type of parameter in () can be written or not written see an exampleCopy the code

package cn.lyn4ever.lambda.exmple;

public class ExampleDemo {public static void main(String[] args) {

        NoResultNoParam noResultNoParam = () -> System.out.println("NoResultNoParam");
        noResultNoParam.test();Copy the code
        NoResultHasParam noResultHasParam = (param) -> System.out.println(param);
        noResultHasParam.test("hello");Copy the code
HasResultHasParam hasResultHasParam = (x, y) -> { int z = x + y; return z; }; {} HasResultHasParam hasResultHasParam1 = (x, y) -> x + y; System. The out. Println (hasResultHasParam. Test (1, 2)); System. The out. Println (hasResultHasParam1. Test (10, 20));Copy the code
    }Copy the code

}

interface NoResultNoParam {void test(); }

interface NoResultHasParam {void test(String param); }

interface HasResultHasParam {int test(int x, int y); } `

This article code has been uploaded to Github, if you feel good, give a star