Why use Stream
1, to bring the benefits of functional programming is particularly evident. This code is more expressed the intention of the business logic, rather than its implementation mechanism. Readable code is also easier to maintain, more reliable and less prone to error.
2, high-end
Examples of data sources
Filter
1, and used through the data checking element therein.
2, filter takes a function as an argument, the function represented by Lambda expressions.
Map
1, map generation is a one to one mapping, for the role
2, the more common
3, and very simple
FlatMap 1, by definition, almost like map, deeper operation
2, but there are still differences
3, map and returns a value different flat
4, Map each input element, according to the rules are converted into another element.
There are some scenes that many mapping relationship, then need flatMap.
5, Map-one
6, Flatmap many
7, method statements and flatMap map is not the same
(1) <r> Stream<r> map(Function mapper);
(2) <r> Stream<r> flatMap(Function> mapper);
(3) map和flatMap的区别:我个人认为,flatMap的可以处理更深层次的数据,入参为多个list,结果可以返回为一个list,而map是一对一的,入参是多个list,结果返回必须是多个list。通俗的说,如果入参都是对象,那么flatMap可以操作对象里面的对象,而map只能操作第一层。
Reduce
1, similar to the feeling of recursive
2, numeral (character string) accumulation
3, the individual did not ye used
Collect
1, collect the list generated in the stream, map, and other commonly used data structures
2、toList()
3、toSet()
4、toMap()
5, custom
Optional
1, Optional data type is a new design for the core class libraries, to replace the null value.
2, there are a lot of people complain about the original null value, even Tony Hoare invention of this concept, too, he said it was one of his "priceless mistake"
3, use very wide, not just in the lambda, which can be used
4, Optional.of (T), T is non-empty, otherwise initialization error
5, Optional.ofNullable (T), T is an arbitrary, can be null
6, isPresent (), equivalent to! = Null
7, ifPresent (T), T may be a period of lambda expressions, or other code, is non-empty execution
Complicated by
1, stream or parallel replaced parallelStream
2, the size of the input stream is not decided whether to parallelize the only factor that will bring faster, the performance will be affected by the number of ways of writing code and nuclear
3, the five elements that affect performance are: the size of the data, source data structure, whether the value of the packing, the number of available CPU core, and a processing time spent for each element
debugging
1, list.map.fiter.map.xx call for the chain, the final call collect (xx) returns the result
2, lazy evaluation points and eager evaluation
3, a judgment operation is inert eager evaluation or evaluation is very simple: it just look return value. If the return value is the Stream, it is then lazy evaluation; if the return value is another value or is empty, is eager evaluation. These operations over the embodiment is formed using lazy evaluation of a chain, with a final value earlier operation request returns the result desired.
4, each value can be viewed by PEEK, can continue to operate while the flow
Reproduced in: https: //www.jianshu.com/p/2960398a6dd0