Function interface and lambda expressions related

0.why lambda?

Code simpler, compact structure.

Concise code Level: external class -> inner classes -> anonymous inner classes -> lambda-> method referenced

Use: usually need a function, but do not want to bother to name the next occasion a function of use, but also refers to anonymous functions

parameters –> a expression;

or (parameters) -> {expressions;}; Note that only a return value.

Parameter may be null: represented by ().

1.lambda achieve expression in the form of java8

Lambda expressions generates an internal class.

Reference Hirofumi https://www.cnblogs.com/WJ5888/p/4667086.html#

This blog post in the analysis of lambda expressions, using the analysis ② ① direct contradiction javap generate bytecode file a proof to back their own ideas and methods of the same name from the method bytecode files obtained by decompilation created their own ;

Side to prove: lambda expressions will first be compiled into a private static function!

Before looking at the bytecode file will be, and bloggers to validate their own ideas based on reductio ad absurdum, here to learn.

Thereafter, the expression produces an internal class.

core java volume, the lambda expression and arranged immediately inside the interface class does appear to have meaning.

Here added -Djdk.internal.lambda.dumpProxyClasses instruction may be generated internal class byte code output to a file.

2. The function object that implements the interface

The object is to achieve the effect of the method as a parameter passed to a process or return

Therefore, the interface should have one and only one method is not implemented, to indicate that the object will pass parameters to achieve any method.

lambda expression can not departing from the function interface independent existence. That is the objective function interface type Lambda expressions.

3. The method references

why method references?

Sometimes when you call a method that already exists only when you create an anonymous method using a lambda expression. Therefore, a lambda expression can be simplified to a reference method.

1. The reference method

①Object:instanceMethod ②Class:staticMethod ③Class:instanceMethod

Method parameters :: method name (parameter list should match the parameter of the process, and the return value should match) ------------------------- -------> method ①②

若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: 类名::实例方法名 --------->方法③ 如下:

Arrays.sort(array,String::compareToIgnoreCase);

Arrays.sort(array,(a,b)->a.compareToIgnoreCase(b));

2. The construction method references

①Class::new ②Class[]::new



import java.util.Arrays;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.stream.Stream;

/**
* author:wanghuanyeah
* 2019/10/26
* 8:37
*/
public class Test {
public static void main(String[] args) {
/* new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
}).start();*/

/* new Thread( ()-> System.out.println("hellowhh")).start();*/

String[] array={"aaaa","bbb","c","36"};
Integer[] is={5,88,66,11,9999};

Comparator<Integer> comparator=(v1,v2)->Integer.compare(v1, v2);
Arrays.sort(is,comparator);
Stream.of(is).forEach(System.out::println);

System.out.println("**************************************************");
Arrays.sort(array, (e1,e2)->Integer.compare(e1.length(), e2.length()));
Stream.of(array).forEach(System.out::println);

System.out.println("**************************************************");
Arrays.sort(array,String::compareToIgnoreCase);
Arrays.sort(array,(a,b)->a.compareToIgnoreCase(b));
Stream.of(array).forEach(System.out::println);

System.out.println("**************************************************");
BiPredicate<String,String> biPredicate=String::equals;
System.out.println(biPredicate.test("wanghuan", "wanghuan"));
}
}

Guess you like

Origin www.cnblogs.com/wanghuanyeah/p/11779225.html