1 Introduction
1) on synchronized multi-core cpu, the need
sync cpu高速缓存
, the need for内核间缓存一致性通信
great price.
2) newStreams流处理
,函数式编程
.
[Processing flow]:java.util.stream
the 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.
3) internal and external iteration iteration
usingfor-each
a processing element and iterations, called external iteration迭代器模式,原集合remove时,会ConcurrentModificationException,使用iterator.remove可以避免。
.
UseStream API
iterate within the library called internal iteration.
2, Lambda Expressions
1) lambda anonymous functions, there is
参数列表、函数主体、返回类型,还有可能抛出的异常列表
.可以在函数式接口上使用lambda表达式
2) Syntax表达式可以隐式返回;语句必须主动return
(parameters) -> expression
or(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方法作为值
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.Predicatefunctional interface函数式接口,函数方法:参数泛型,返回boolean值( 输入的对象是否 符合某个条件)
函数描述符:boolean test(T t) 接受泛型参数,返回boolean值。
3)java.util.function.Consumer
funtional interface函数式接口,函数方法:参数泛型,返回值void(需要访问T,并对其执行某些操作时使用。)
4)java.utils.function.Function
接受一个参数,产出一个结果,两者类型可能不同
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