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
3.1.2 parallelStream() method
Parallel stream
3.2 Through the stream() method of Arrays
3.3 Through the static method of() in the Stream class
3.4 Infinite Stream
3.4.1 Iteration
According to the initial value of 0 and +2 operation rules, unlimited creation
3.4.2 Generate
Use the supply function interface to generate random numbers indefinitely
4. Intermediate operation
Intermediate operations can only be processed all at once when the operation is terminated
4.1 Screening and slicing
4.2 Mapping
Capitalize each character.
Perform a getName operation on each element in the employees collection.
flatMap extracts all the elements and puts them into a stream.
4.3 Sort
Natural sorting
Custom sorting
5. Terminate operation
5.1 allMatch
5.2 anyMatch
5.3noneMatch
5.4 findFirst
Return an optional container
5.5 findAny
5.6 count
5.7 max
5.8 min
5.9 reduce reduce
Given an initial value, sum the elements in the set. Reduce
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.
The combination of map and reduce is called map-reduce mode.
5.10 collect
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.
l Use the set feature to remove duplicates and
pass in a specific set.
Other methods:
total number
average value
sum
maximum value
minimum value
grouping
multi-level grouping
according to conditions, meeting conditions and not meeting conditions. The three parameters of
character splicing
are segmentation, the first and the last. character