First, create stream
Create stream mode one: Stream () or Parallelstream () of the collection class
java List<String> list = new ArrayList<>(); Stream<String> stream = list.stream();
Create stream mode two: Get through the static method stream () in arrays
new String[10];Stream<String> stream1 = Arrays.stream(strings);
Create stream mode three: by static method in the Stream class of ()
Stream<String> stream2 = Stream.of("aa""bb""cc");
Create stream mode four: infinite stream
Iteration:
Stream<Integer> stream3 = Stream.iterate(02);
Generated:
Stream.generate(() -> Math.random());
Second, intermediate operation
Filtering and slicing
Filter accepts a lambda and filters out the element that satisfies the condition from the stream.
Limit limits the number of elements in the Stram. After the limit quantity is met, later iterations in the stream terminate, similar to the short-circuit operation.
Skip (n) skips the element and returns a stream that throws out the first n elements. If there are less than n elements in the stream, an empty stream is returned. Complementary to limit (n)
Distinct removes duplicate elements through Hashcode () and Equels () of elements in stream
Mapping
Map accepts lambda, transforms elements into other forms or extracts information. Takes a function as an argument, and the function is applied to each element, and it is mapped to a new element.
FlatMap takes a function as an argument, replaces each value in the stream with another stream, and then connects all the streams into a stream.
Sort
Sorted () Natural sort sorted by comparable
Sorted (Comparator com) custom sort sort by Comparator
Return to about
t reduce (t identity, binaryoperator
- Collect
Optional
- Internal iterations: Iterative operations are done by the stream API
- External iterations: Write your own iteriator
Third, parallel stream and serial stream
A parallel stream is one that divides a piece of content into chunks of data and processes each stream of data blocks separately with different threads. Java 8 optimizes parallelism, and we can easily parallelize the data. The stream API can declaratively switch between a parallel stream and a sequential stream through parallel () and sequential ().
Java8 new Features--stream