With so many functions in SQL, why does Java 8 provide repeated stream methods to kill one stone?

Time:2022-5-9

A classmate raised such a question;

In business systems, data is generally queried from SQL, such as where, order by, limit, aggregation function, etc. why use java8 stream method?

First, we can look at the method of stream.

Three ways to create stream

  • AssembleCollection.stream()
  • Static methodStream.of
  • ArrayArrays.stream

Termination of stream

  • foreach(Consumer c)Traversal operation
  • collect(Collector)Convert flow to other forms
  • max(Comparator)Maximum value in reflux
  • min(Comparator)Minimum value in reflux
  • countReview of elements in reflux

Collectors specific methods

  • toList List<T>Collect the elements in the stream into a list
  • toSet Set<T>Collect the elements in the stream into a set
  • toCollection Coolection<T>Collect the elements in the stream into the collection
  • groupingBy Map<K,List<T>>Group streams according to K attribute
  • partitioningBy Map<boolean, List<T>>Group according to Boolean value

Intermediate operation of stream

  • filter(Predicate)Filter some elements in the stream
  • map(Function f)Receive the element in the stream and map it into a new element, for example, take the name attribute from the student objectflatMap(Function f)Join all the elements in the flow together to form a flow
  • peek(Consumer c)Get the elements in the flow and operate the elements in the flow. Unlike foreach, the flow will not be truncated and the operation flow can be continued
  • distinct()De duplication through the equals and hashcode of the elements generated by the stream
  • limit(long val)Truncate the stream and get the first Val element in the stream
  • sorted(Comparator)Generate a new stream and sort it according to the comparator rules
  • sorted()Generate a new stream and sort it in natural order

matching

  • booelan allMatch(Predicate)All meet
  • boolean anyMatch(Predicate)Any element conforms to
  • boolean noneMatch(Predicate)None of them

The above lists some common operation methods of stream (). In fact, most of them can be implemented in SQL. At the same time, similar to Oracle, MySQL and other databases, countless cows have spent more than ten years to help us optimize these operations, bringing their own optimized indexes and caches. Therefore, if SQL processing is used conditionally in the case of large amount of data, it is theoretically faster than the method provided by stream.

Most of the functions of stream overlap the functions and methods of SQL. In this case, why does Java 8 provide such APIs? You can’t do it efficiently?

Stream has its own usage scenarios. Now microservices are becoming more and more popular. A system is divided into n services, and each service uses a different database, but there are often data intersections between different services. In this case, the processing on SQL will become very troublesome.

For example, there is now a service (member management), which stores member information, including member level, registration date, recent consumption, etc. B service (registered user management), which stores the basic information of users, including address, contact number, birthday, etc; To get an overview of member information, VVIP members need to send a MMS blessing and a worthless inventory item as a gift on their birthday.

When you count or summarize member information, you can’t use an SQL to do it. At this time, stream grouping, filtering, matching and other functions play a role. It may take a lot of time to implement these functions with javastream 8, but it may take a lot of time to implement these functions with javastream.

For example:

Screen members older than 18

List<VIPUsers> collect = lists.stream().filter(e -> e.getAge() > 18).collect(Collectors.toList());

Group by member type

Map<String, List<VIPUsers>> collect3 = lists.stream()
        .collect(Collectors.groupingBy(vUser::getType));

Total statistics

long count = lists.stream().count();

Can be very convenient to operate.

Stream is the optimization of collection operation in Java 8. Compared with iterator, using stream is very fast, and it supports parallel processing of data in collection. By default, it can make full use of CPU resources. At the same time, it supports functional programming, and the code is very concise. Using stream in daily development makes the development efficiency faster and the code more concise.

However, it should be noted that do not rely too much on stream operations, commonly used filtering, aggregation, grouping and other operations. If both stream and SQL can be processed, it is more recommended to use SQL for operations, especially in the case of large amount of data, make full use of the cache and index of the database, and don’t think about optimizing yourself.

Lao Pan said, uncle, I’m afraid you can’t grasp it, son, let the database come!!!

Ten recommendations

[issue 271] Interviewer: what is the processing flow of spring MVC?

[issue 272] data structure: the principle of hash table and common test sites in interview

[issue 273] tell the interviewer that I can optimize groupby and know it very well!

[274] Interviewer: how to ensure the consistency of cache and database

[275] Interviewer: how much do you know about indexes in MySQL?

[issue 276] Interviewer: have you analyzed the implementation principle of @ annotation annotation?

[issue 277] Interviewer: talk about several common distributed ID solutions

[issue 278] Interviewer: they all say that select * is inefficient. Do you know why?

[issue 279] Interviewer: how many ways can Java traverse the map set? How about their efficiency?

[issue 280] what do you ask in k8s interview?

With so many functions in SQL, why does Java 8 provide repeated stream methods to kill one stone?