Java 8 中 Stream API

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

13145050-fc121b6bea592d18
image

Filter

1, and used through the data checking element therein.

2, filter takes a function as an argument, the function represented by Lambda expressions.

13145050-6f89f4d61f863a26
image
13145050-fd3f6a08eadd1f1f
image

Map

1, map generation is a one to one mapping, for the role

2, the more common

3, and very simple

13145050-ce23c711ab73970b
image
13145050-46617c5db7c9b276
image

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只能操作第一层。
13145050-ecd9ba9b510ca76b
image
13145050-fdb5cc1a3797a680
image

Reduce

1, similar to the feeling of recursive

2, numeral (character string) accumulation

3, the individual did not ye used

13145050-e5ef44027051c07b
image
13145050-9054b54d136a4706
image

Collect

1, collect the list generated in the stream, map, and other commonly used data structures

2、toList()

3、toSet()

4、toMap()

5, custom

13145050-aebb4ddb9bc2a7d1
image

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

13145050-f4b3ad1d517da8b8
image

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

13145050-43f4e0b05a1c0a5d
image

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

13145050-b04049dda9b5f309
image

Reproduced in: https: //www.jianshu.com/p/2960398a6dd0

Guess you like

Origin blog.csdn.net/weixin_34090643/article/details/91243244