Java8 Stream 一行代碼實現(xiàn)數(shù)據(jù)分組統(tǒng)計、排序、最大值、最小值、平均值、總數(shù)、合計
閱讀本文大概需要 5 分鐘。
來自:blog.csdn.net/xiaoheihai666/article/details/128152182
-
示例:統(tǒng)計用戶status的最大值,最小值,求和,平均值 -
分組統(tǒng)計: -
如果我們想看某個部門下面有哪些數(shù)據(jù),可以如下代碼 -
求最大值,最小值 -
對某個字段求最大,最小,求和,統(tǒng)計,計數(shù) -
求最大值,最小值還可以這樣做 -
對某個字段求和并匯總 -
求某個字段的平均值 -
拼接某個字段的值,可以設(shè)置前綴,后綴或者分隔符 -
根據(jù)部門進行分組,并獲取匯總?cè)藬?shù) -
根據(jù)部門和是否退休進行分組,并匯總?cè)藬?shù) -
根據(jù)部門和是否退休進行分組,并取得每組中年齡最大的人
-
getAverage(): 它返回所有接受值的平均值。 -
getCount(): 它計算所有元素的總數(shù)。 -
getMax(): 它返回最大值。 -
getMin(): 它返回最小值。 -
getSum(): 它返回所有元素的總和。
示例:統(tǒng)計用戶status的最大值,最小值,求和,平均值
@GetMapping("/list")
public void list(){
List<InputForm> inputForms = inputFormMapper.selectList();
Map<String, IntSummaryStatistics> collect = inputForms.stream()
.collect(Collectors.groupingBy(InputForm::getCreateUserName, Collectors.summarizingInt(InputForm::getStatus)));
// 對名字去重
Set<String> collect1 = inputForms.stream().distinct().map(InputForm::getCreateUserName).collect(Collectors.toSet());
// 遍歷名字,從map中取出對應(yīng)用戶的status最大值,最小值,平均值。。。
for (String s1 : collect1) {
IntSummaryStatistics statistics1 = collect.get(s1);
System.out.println("第一個用戶的名字為====" + s1);
System.out.println("**********************************************");
System.out.println("status的個數(shù)為===" + statistics1.getCount());
System.out.println("status的最小值為===" + statistics1.getMin());
System.out.println("status的求和為===" + statistics1.getSum());
System.out.println("status的平均值為===" + statistics1.getAverage());
System.out.println();
System.out.println();
}
}
分組統(tǒng)計:
@GetMapping("/list")
public void list(){
List<InputForm> inputForms = inputFormMapper.selectList();
System.out.println("inputForms = " + inputForms);
Map<String, Long> collect = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateUserName,
Collectors.counting()));
System.out.println("collect = " + collect);
}
? 其中Collectors.groupingBy(InputForm::getCreateUserName, Collectors.counting())返回的是一個Map集合,InputForm::getCreateUserName代表key,Collectors.counting()代表value,我是按照創(chuàng)建人的姓名進行統(tǒng)計 ?
如果我們想看某個部門下面有哪些數(shù)據(jù),可以如下代碼
@GetMapping("/list")
public Map<String, List<InputForm>> list(){
List<InputForm> inputForms = inputFormMapper.selectList();
System.out.println("inputForms = " + inputForms);
Map<String, List<InputForm>> collect = inputForms.stream()
.collect(Collectors.groupingBy(InputForm::getCreateCompanyName));
return collect;
}
求最大值,最小值
@GetMapping("/list")
public Map<String, List<InputForm>> list(){
List<InputForm> inputForms = inputFormMapper.selectList();
System.out.println("inputForms = " + inputForms);
Optional<InputForm> min = inputForms.stream()
.min(Comparator.comparing(InputForm::getId));
System.out.println("min = " + min);
return null;
}
對某個字段求最大,最小,求和,統(tǒng)計,計數(shù)
@GetMapping("/list")
public void list(){
List<InputForm> inputForms = inputFormMapper.selectList();
System.out.println("inputForms = " + inputForms);
IntSummaryStatistics collect = inputForms.stream()
.collect(Collectors.summarizingInt(InputForm::getStatus));
double average = collect.getAverage();
int max = collect.getMax();
int min = collect.getMin();
long sum = collect.getSum();
long count = collect.getCount();
System.out.println("collect = " + collect);
}
求最大值,最小值還可以這樣做
// 求最大值
Optional<InputForm> max = inputForms.stream().max(Comparator.comparing(InputForm::getAgency));
if (max.isPresent()){
System.out.println("max = " + max);
}
// 求最小值
Optional<InputForm> min = inputForms.stream().min(Comparator.comparing(InputForm::getAgency));
if (min.isPresent()){
System.out.println("min = " + min);
}
對某個字段求和并匯總
int sum = inputForms.stream().mapToInt(InputForm::getStatus).sum();
System.out.println("sum = " + sum);
求某個字段的平均值
// 求某個字段的平均值
Double collect2 = inputForms.stream().collect(Collectors.averagingInt(InputForm::getStatus));
System.out.println("collect2 = " + collect2);
// 簡化后
OptionalDouble average = inputForms.stream().mapToDouble(InputForm::getStatus).average();
if (average.isPresent()){
System.out.println("average = " + average);
}
拼接某個字段的值,可以設(shè)置前綴,后綴或者分隔符
// 拼接某個字段的值,用逗號分隔,并設(shè)置前綴和后綴
String collect3 = inputForms.stream().map(InputForm::getCreateUserName).collect(Collectors.joining(",", "我是前綴", "我是后綴"));
System.out.println("collect3 = " + collect3);
根據(jù)部門進行分組,并獲取匯總?cè)藬?shù)
// 根據(jù)部門進行匯總,并獲取匯總?cè)藬?shù)
Map<String, Long> collect4 = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateDeptName, Collectors.counting()));
System.out.println("collect4 = " + collect4);
根據(jù)部門和是否退休進行分組,并匯總?cè)藬?shù)
// 根據(jù)部門和是否退休進行分組,并匯總?cè)藬?shù)
Map<String, Map<Integer, Long>> collect5 = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateDeptName, Collectors.groupingBy(InputForm::getIsDelete, Collectors.counting())));
System.out.println("collect5 = " + collect5);
根據(jù)部門和是否退休進行分組,并取得每組中年齡最大的人
// 根據(jù)部門和是否退休進行分組,并取得每組中年齡最大的人
Map<String, Map<Integer, InputForm>> collect6 = inputForms.stream().collect(
Collectors.groupingBy(InputForm::getCreateDeptName,
Collectors.groupingBy(InputForm::getIsDelete,
Collectors.collectingAndThen(
Collectors.maxBy(
Comparator.comparing(InputForm::getAge)), Optional::get))));
System.out.println("collect6 = " + collect6);
推薦閱讀:
SELECT COUNT(*) 會造成全表掃描?回去等通知吧
Spring 在多線程環(huán)境下如何確保事務(wù)一致性?
互聯(lián)網(wǎng)初中高級大廠面試題(9個G) 內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)取! 朕已閱

