I started writing the first issue today. I hope I can keep going.

In the process of reviewing code, I would often see some code that could be handled better with stream, and would advise colleagues to use stream.

Stream is a feature provided by Java 8 for many years, but some programs are still reluctant to use in the development. They think the code is not straightforward enough, and they prefer to write more for loops. However, history proves that new favorites are always better than old ones. Don’t touch, learn new things, will become an antique. If you try to get out of your car and take a piss, you’ll be thrown under the wheel of history because it’s a high-speed train. You see the new love is waving to you.

1. Why not use streams

I think there are several reasons:

First, the current “for while” can solve the problem, there is no need

Second, they are not willing to use stream because they do not find it good.

Third, they did not grasp the core of understanding, memory is very difficult to use.

2. Understand flow

Streams are literally processed sequentially to produce results.

As I understand it, streams and SQL behave in a similar way. Filter and integrate data.

 

As a Java programmer, you generally know how to write Sql. A simple Sql would look like this:

Select * from table where group by Limit 10 Filter out the data from the data source, group the data, and obtain part of the data.Copy the code

Streams can do almost anything SQL can do.

3. Take an example

 

Define the Player class as follows:

/**
 * 玩家类 
 * 公众号:香菜聊游戏
 */
@AllArgsConstructor
public class Player {
    //  名字
    @Getter @Setter
    public String name;
    //  性别
    @Getter @Setter
    private int sex;
    //  年龄
    @Getter @Setter
    private int age;
    //  工资
    @Getter @Setter
    private int salary;

}
Copy the code

1, Filter data ==>where

List<Player> age18List = playerlist.stream ().filter(Player -> player.getage () > 18) .collect(Collectors.toList());Copy the code

Select * from ‘select’

List<String> age18NameList = playerList.stream()
                .filter(player -> player.getAge() > 18)
                .map(Player::getName)
                .collect(Collectors.toList());
Copy the code

Group by sex ==> group by sex ==> group by sex ==> group by sex

Map<Integer, List<Player>> age18GroupMap = playerList.stream()
                .filter(Player -> Player.getAge() > 18)
                .collect(Collectors.groupingBy(Player::getSex));
Copy the code

4. Select the first two digits after sorting ==> LIMIT

Sorted (Comparator.comparingInt(Player::getAge)).sorted(Comparator.comparingInt(Player::getAge) .limit(2) .collect(Collectors.toList());Copy the code

5, sum ==> sum

Integer age18SumSalary = playerList.stream()
                .map(Player::getSalary)
                .reduce(0,Integer::sum);
Copy the code

The above examples are just a few examples of streams and their similarities to SQL. You can do anything you want. Streams that are difficult to implement in SQL can also be implemented. In the study of the time to understand first, in the use of time is not clear can search, multi-purpose a few times with respect to proficiency, master. Stream adds nothing new, just a new wine in a new bottle, with different grammatical forms.

The choice between “love me” and “hate me” is always there… Hate me, because I am beautiful, I am confident to hubris, hate me because I am accurate fadeaway jumper, I desire for victory, hate me because I’m a veteran, that do a winner Hate me, please with every cell in your body to hate me, hate me are the same, persistence, a group of people deeply in love with

It’s exactly like yours…

4, summarize

Stream makes our code cleaner, logically smoother, writes less code and does more. Stream decouples the code details from the business logic, expresses “what to do” instead of “how to do it”, and can focus more on business logic and write code that is easy to understand and maintain. Using SQL knowledge as an analogy, to complete the understanding and learning of stream, knowledge transfer is a shortcut for us to learn, have you mastered it?

There are willing to join the sharing of big guy contact me!!

Have not understood can private chat me, study together exchange.

Finally, share the book advanced JVM Features and Best Practices. Reply to “Advanced JVM Features and Best Practices” for a link.