Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Java8 provides a way of streaming computation for collections, in which the collection of elements to be processed is thought of as a stream, which is transmitted in a pipe and can be processed on the nodes of the pipe, such as filtering, sorting, aggregating, and so on.

The Stream API basically returns the Stream itself, so multiple operations can be concatenated into a single channel, as in Fluent style. This can optimize the operation, as compared to precluding or short-circuiting.

  • Stream () creates a serial stream for the collection
  • ParallelStream () creates parallel streams for collections
private void createStream(a){
    Stream<Integer> stream = getList().stream();
    Stream<Integer> parallelStream = getList().parallelStream();
}
Copy the code

Stream provides an internal traversal method, forEach(), that can greatly simplify the code for aggregate traversal.

private void forEach(a){
    getList().forEach(i->System.out.println(i));
}
Copy the code

Stream provides a filter() method for data filtering that can be easily implemented in conjunction with other Stream apis.

Stream provides a method called map() for data mapping that can be easily implemented in conjunction with other Stream apis.

Stream provides a sorted() method for data mapping that, in conjunction with other Stream apis, makes sorting data easy.

The Collectors class implements many reduction operations, such as replacing flows with collections and aggregation elements. Collectors can be used to return lists or strings.

Some collectors that produce statistics are also useful. They are used primarily for basic types like int, double, and long, and they can be used to produce statistics like the following.