Stream API in java8

Stream API in java8

1. Understand

Perform a series of operations on the data source during stream in java8, and finally return a stream
stream

  • No data will be stored
  • Do not change the source data
  • The operation is executed in a delayed manner, and will not be executed until the result is needed

2. Operation steps

  • Create Stream
  • Intermediate operations on data
  • Terminate operation

3. Create a stream

3.1 Methods provided by Collection

3.1.1 stream() method

Serial stream
Insert picture description here

3.1.2 parallelStream() method

Parallel stream

3.2 Through the stream() method of Arrays

Insert picture description here

3.3 Through the static method of() in the Stream class

Insert picture description here

3.4 Infinite Stream

3.4.1 Iteration

According to the initial value of 0 and +2 operation rules, unlimited creation
Insert picture description here
Insert picture description here
Insert picture description here

3.4.2 Generate

Use the supply function interface to generate random numbers indefinitely
Insert picture description here

4. Intermediate operation

Intermediate operations can only be processed all at once when the operation is terminated
Insert picture description here

4.1 Screening and slicing

Insert picture description here
Insert picture description here

4.2 Mapping

Insert picture description here
Capitalize each character.
Insert picture description here
Perform a getName operation on each element in the employees collection.
Insert picture description here
flatMap extracts all the elements and puts them into a stream.

4.3 Sort

Insert picture description here

Insert picture description here
Natural sorting
Insert picture description here
Insert picture description here
Custom sorting
Insert picture description here
Insert picture description here

5. Terminate operation

Insert picture description here

5.1 allMatch

Insert picture description here

5.2 anyMatch

Insert picture description here

5.3noneMatch

Insert picture description here

5.4 findFirst

Return an optional container
Insert picture description here

5.5 findAny

Insert picture description here

5.6 count

Insert picture description here

5.7 max

Insert picture description here

5.8 min

Insert picture description here

5.9 reduce reduce

Insert picture description here
Given an initial value, sum the elements in the set. Reduce
Insert picture description here
without an initial value, in order to prevent the return value from being empty, the return value type is optional.
Seeking the sum of the salaries of all employees in the company.
Insert picture description here
The combination of map and reduce is called map-reduce mode.

5.10 collect

Insert picture description here
The incoming type is collector, and you can use the collecetors tool class to call a static method to pass in.
Return the names of all employees and encapsulate them in the list.
Insert picture description here
l Use the set feature to remove duplicates and
Insert picture description here
pass in a specific set.
Insert picture description here
Other methods:
total number
average value
sum
maximum value
minimum value
grouping
Insert picture description here
Insert picture description here
multi-level grouping
Insert picture description here
according to conditions, meeting conditions and not meeting conditions. The three parameters of
Insert picture description here
character splicing
Insert picture description here
are segmentation, the first and the last. character
Insert picture description here

Guess you like

Origin blog.csdn.net/Guesshat/article/details/113072107