Stream flow [] advanced usage - grouping sum, the combination of the sort, seeking extreme value

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/cxh6863/article/details/102650492

Pass through the blog (https://blog.csdn.net/cxh6863/article/details/102634781), based on the use of the Stream, we should no longer strange to stream Stream, Stream will then learn advanced usage under the bar.

A packet summing -collect

Scenes:

In a class, everyone has their own score, how to use the code in java to score all based on gender, and then summed.

method:

The method used to collect collected list into the map, and then classified using groupby,
GetSum () - Get and
getMax () - Get maximum
getMin () - Get minimum
getAverage () - Get average
getCount () - Gets the amount of data

use:

System.out.println("分组求和:");
Map<String, IntSummaryStatistics> collectGroupBySex = students.stream().collect(Collectors.groupingBy(student::getSex, Collectors.summarizingInt(student::getScore)));
System.out.println("男生:"+collectGroupBySex.get("男"));
System.out.println("男生的分数和:"+collectGroupBySex.get("男").getSum());
System.out.println("男生中最大分数:"+collectGroupBySex.get("男").getMax());
System.out.println("男生中最小分数:"+collectGroupBySex.get("男").getMin());
System.out.println("男生中平均分数:"+collectGroupBySex.get("男").getAverage());
System.out.println("男生个数:"+collectGroupBySex.get("男").getCount());

operation result

Here Insert Picture Description

Second, the combination of the sort -comparing-> thenComparing

Scenes:

In a class, everyone has their own score, how to use the code for all of the first fraction and then sorted according to gender sorted by score in java.

method:

comparing,thenComparing

use

System.out.println("组合排序:");
System.out.println("第一种:");
List<student> collectSortedMore = students.stream().sorted(Comparator.comparing(student::getSex).reversed().thenComparing(student::getScore).reversed()).collect(Collectors.toList());
collectSortedMore.stream().forEach(System.out::println);
System.out.println("第二种:");
students.sort(Comparator.comparing(student::getSex).reversed().thenComparing(student::getScore).reversed());
students.stream().forEach(student -> System.out.println(student));

operation result

Here Insert Picture Description

Third, seeking extreme value -summarizingLong / summarizingInt

Scenes:

In a class, everyone has their own score, how to use the code in java average seek this class, maximum points and minimum points.

method:

First use summarizingLong and summarizingInt to score summary, then get extreme value respectively

Obtaining an average value --getAverage ()
Get the maximum --getMax ()
Gets the minimum --getMin ()
Get out --getSum ()
Get the number of --getCount ()

use

System.out.println("求极值:");
LongSummaryStatistics collectStudentsSummary = students.stream().collect(Collectors.summarizingLong(student::getScore));
System.out.println("平均值=" + collectStudentsSummary.getAverage());
System.out.println("个数=" + collectStudentsSummary.getCount());
System.out.println("最大值=" + collectStudentsSummary.getMax());
System.out.println("最小值=" + collectStudentsSummary.getMin());
System.out.println("总和=" + collectStudentsSummary.getSum());

operation result

Here Insert Picture Description

Finally on stream and for, the following is a personal understanding:
stream and for comparison loop
for loop is a cycle of operation in order to perform
stream is carried out on one asynchronous operation
that is to say for this one operation cycle is over, then the next this operation is performed once
while the stream is almost at the same time all operations
such as the need for an operation cycle ten times, for a time loop is executed, executed 10 times, 10 times a stream is simultaneously executed 10 times.

Guess you like

Origin blog.csdn.net/cxh6863/article/details/102650492