Java8 core point

1 Introduction

1) on synchronized multi-core cpu, the need sync cpu高速缓存, the need for 内核间缓存一致性通信great price.
2) new Streams流处理, 函数式编程.
[Processing flow]: java.util.streamthe new Stream <T> of the api. Similar to the linux command processing streams, and use the pipe | connected. cat file1.txt file2.txt | grep "my word" | head -10, Streams can make better use multicore cpu, parallel processing.
[Functional programming]: 行为参数化,把代码传递给方法,允许将方法作为值。原先只能通过匿名类给方法传递函数功能。
The method of quoting syntax :: 例如 File::isHidden 会创建一个方法引用,进而作为值进行传递。
[Interface] default method: 接口可以包含实现类没有提供的方法,使用default关键字标名
[Other]: Lambda 匿名函数and Optional <T> avoid a null value.

5148056-c37de42472512d0b.png
image.png

3) internal and external iteration iteration
using for-each a processing element and iterations, called external iteration 迭代器模式,原集合remove时,会ConcurrentModificationException,使用iterator.remove可以避免。 .
Use Stream API iterate within the library called internal iteration.

2, Lambda Expressions

1) lambda anonymous functions, there is 参数列表、函数主体、返回类型,还有可能抛出的异常列表.
可以在函数式接口上使用lambda表达式
2) Syntax 表达式可以隐式返回;语句必须主动return
(parameters) -> expressionor (parameters) -> { statements; } 注意花括号和语句结尾符;
(String s) -> s.length()
(Apple a) -> a.getWeight() > 10
(X int, int Y) -> {
System.out.println ( "X:" X +);
System.out.println ( "Y:" + Y);
}
() -> "result"
(Apple a, Apple b) -> a.getWeight().compareTo(b.getWeight())
当只有一个参数,且其类型可推导时,圆括号()可省略。如: a -> return a*a
. 3) and the lambda anonymous inner classes方法作为值

5148056-560a5f9bc42b6868.png
image.png

4) lambda expressions: can 赋值给一个变量 ; 作为参数 delivery method that accepts a functional interface ( lambda表达式的签名,需要与函数式接口的默认方法的签名一致 ).
Function descriptor (Signed): () -> void 表示参数为空,返回值为void的函数签名 .
(Apple a, Apple b) -> int 表示接受两个Apple作为参数,返回int的函数签名 .
5) in support of a class of functions of language, Lambda 表达式的类型是函数 ; in Java, Lambda 表达式是对象 they have 依附于 a special category of object types - 函数式接口(functional interface) .
6) the difference between lambda and anonymous inner classes
1.对于this关键字,匿名内部类解析为内部类;lambda表达之解析为lambda的外部类。
2.java编译器将lambda表达式转换类类的私有函数;匿名内部类编译为.class文件。

3, the function interface

1) Definition: 只定义了一个抽象方法的接口,使用了@FunctionalInterface注解()。如java.lang.Runnable、java.util.concurrent.Callable
Function Descriptor: 函数式接口的抽象方法的签名(方法名+形参列表)。
2) java.util.function.Predicate
functional interface函数式接口,函数方法:参数泛型,返回boolean值( 输入的对象是否 符合某个条件)
函数描述符:boolean test(T t) 接受泛型参数,返回boolean值。

5148056-d61deec329cb1e9c.png
image.png

5148056-a5f2aa25bd81ae60.png
image.png

3)java.util.function.Consumer
funtional interface函数式接口,函数方法:参数泛型,返回值void(需要访问T,并对其执行某些操作时使用。)
5148056-4e35d82b20719b40.png
image.png

5148056-3ca25604fadbe677.png
image.png

4)java.utils.function.Function
接受一个参数,产出一个结果,两者类型可能不同
5148056-66f6f9a913608495.png
image.png

5148056-85af9f739f381220.png
image.png

4, reference method

1) the definition of reuse existing methods, can be seen as syntactic sugar lambda expressions used ::.

Type 2) a method reference 不需要(),因为并没有实际调用
point 类的静态方法of the reference Integer::parseInt
point 类的实例方法of the reference String::length
point to 对象的实例方法reference myService.query(userId)
3) and corresponding references type of lambda

5148056-305a0a730d7a94ce.png
image.png

Guess you like

Origin blog.csdn.net/weixin_33919950/article/details/90893822