This is the tenth day of my participation in the August More text Challenge. For details, see:August is more challenging

The paper

Stream objects are a post-Java 8 convenient set of operations to filter, sort, aggregate, and so on

Stream can be divided into two modes of operation (I find this part is very similar to mysql’s transaction isolation level, just a personal opinion).

Stream Operation classification
Intermediate Operations Stateless (Stateless) unordered() filter() map() mapToInt() mapToLong() mapToDouble() flatMap() flatMapToInt() flatMapToLong() flatMapToDouble() peek()
Stateful (Stateful) distinct() sorted() sorted() limit() skip()
Terminal Operations Non-short circuit operation forEach() forEachOrdered() toArray() reduce() collect() max() min() count()
Short-circuit operation anyMatch() allMatch() noneMatch() findFirst() findAny()

Intermediate operation: it is just a marker and does not calculate the final result

  • Stateless: an element whose processing is unaffected by previous element operations (that is, uncommitted does not affect subsequent operations)
  • Stateful: The operation must be operated only after all elements have acquired a concept similar to a lock. Of course, this is not so strict as lock, just borrow the noun lock to help yourself understand

End operation: only at the end of the actual result calculation + non-short circuit operation: need to deal with all elements to get the result, such as select all boys from 100 people + short circuit operation: as long as find the condition can get the result, choose one boy from 100 people

So a complete stream expression uses the intermediate operation to do some calculation, and then the end operation to count the results. For example: take a boy from a hundred people. The intermediate operation is to determine which boys are, and the end operation is to return the first boy you get and end the process

Simple use — Simple use

create

List.stream () creates a sequential stream

List.parallelstream () creates a parallelStream(which processes the collection of processes in a way that is consistent with the concept of a sequential stream except for parallel execution), a parallelStream needs to consider whether the parallelism efficiency must be high, and some shared state of resources, etc. In addition, we need to determine that the processing of data in the stream cannot be sequential, such as I want to find the first boy. Parallel execution You know which boy is returning (for example, don’t bother). You can use list.stream().parallel() to replace a stream sequence with a stream parallel

Array. The stream (Array) to create

Stream static methods “Stream. Of “, “Stream. Iterate “, “Stream.

Intermediate operation – filtering

filter

As the name implies, a filter operation is performed, and after the flow is filtered, only the elements that meet the filter conditions are retained

Students. The stream (). The filter (student - > student. GetSex = = boys). End Operation ()

Accepts a functional interface, Predicate, as an incoming parameter, filtering the data in the stream.

distinct

To repeat

Stream.of("1","2","1","3").distinct(). End Operation ()

Skip, the parameter is the element to be skipped, and the subscript starts at 1

Of (“a”,”b”,” C “)

Limit Gets the element specified by one of the parameters

Stream of (" a ", "b", "c"). The limit (1, 2)

The intermediate operation — mapping

List

Names =students.stream().map(Student::getName())

flatMap

The only difference is that it still returns a Stream object

End operation – reduction

Do some aggregation of all parameters (similar to Mysql’s aggregation function, many pieces of data, return one or one data)

List.stream ().reduce(Integer::sum)

personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(), Integer::max)

Finish the operation — Get the result

Stu =students.stream().filter(Student -> student.getsex ==” boy “).findFirst();

FindAny () returns an object that arbitrarily retrieves one of the elements

anyMatch()

Return Boolean to determine whether an element of the decision rule is included (whether there are boys)

allMatch()

Return Boolean to determine whether all of them satisfy the decision rule (whether all are boys)

noneMatch()

Return Boolean to determine if there are no boys that satisfy the decision rule.

collect

Where the parameters are similar to toList(), toSet, and toMap

In simple terms, we will collect the result of the previous calculation and return the result object of the corresponding parameter type

personList.stream().filter(p -> p.getSalary() > 8000) .collect(Collectors.toMap(Person::getName, p -> p))

count

Similar to mysql’s count, where the parameter indicates the field whose statistics are to be collected

sorted

The sorting

Stream<T> sorted();
Stream<T> sorted(Comparator<? super T> comparator);

Stream.of("3", "1", "2").sorted().forEach(System.out::println);
Copy the code