Java operates List according to a certain element grouping method

For example, there is the following data in the database table:
Xiaohong girl is 14 years old,
Xiaoming boy is 12 years old,
Xiaolin boy is 13 years old,
Xiao Song girl is 12 years old

Fish out all the data as a List<Student>

At this time, these data need to be grouped according to gender, that is, divided into two Lists according to boys and girls respectively.

How to do it?

Go directly to the code:

Map<String, List<Student>> mapGroup = list.stream().collect(Collectors.groupingBy(Student::getGender));

Here the Student class is the student class, the Gender attribute is gender, and the String type is "male" and "female".

Guess you like

Origin blog.csdn.net/GBS20200720/article/details/127582615