Java8 Stream流多字段求和、汇聚的实例
作者:我想写游戏 发布时间:2023-03-14 20:17:23
标签:Java8,Stream,字段求和
Stream流多字段求和、汇聚
实现方法
利用
Collectors.toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)
keyMapper
:代表你最终想要获得的Map<Key, Value> 的KeyvalueMapper
:代表你最终想要获得的Map<Key, Value> 的ValuemergeFunction
:表示碰到Key冲突是处理过程,{x, y}中x是已汇聚对象,y表示当前处理对象
对象类型数据处理
public static Map<String, Model> streamGroupSum(List<Model> datas){
return datas.stream().collect(Collectors.toMap(k -> k.getCode(), v -> v, (x, y) -> x.addCount().addAll(y)));
}
Model
@Data
class Model{
private String code;
private int count = 0;
private Integer sum1;
private Integer sum2;
public Model(String code, Integer sum1, Integer sum2){
this.code = code;
this.sum1 = sum1;
this.sum2 = sum2;
}
public Model addCount(){
this.count++;
return this;
}
public Model addAll(Model y){
return add(Model::setSum1, Model::getSum1, y)
.add(Model::setSum2, Model::getSum2, y);
}
/**
* 使用函数式编程,最终目的是为了求和,类似反射,具体使用方式请移步函数式编程
*/
public Model add(BiConsumer<Model, Integer> set, Function<Model, Integer> get, Model y){
set.accept(this, get.apply(this) + get.apply(y));
return this;
}
}
Map类型数据处理
public static void main (String[] args) {
List<Map<String, Object>> datas = getDatas();
streamMapSum(datas);
}
public static Map<Object, Map<String, Object>> streamMapSum (List<Map<String, Object>> datas) {
return datas.stream()
.collect(Collectors.toMap(k -> k.get("name"), v -> {
v.put("count", 1);
return v;
}
, (x, y) -> {
x.put("count", (int) x.get("count") + 1);
x.put("aaa", (int) x.get("aaa") + (int) y.get("aaa"));
x.put("bbb", (int) x.get("bbb") + (int) y.get("bbb"));
x.put("ccc", (int) x.get("ccc") + (int) y.get("ccc"));
return x;
/*
//使用ofMap重构
return ofMap("name", x.get("name")
, "count", (int) x.get("count") + 1
, "aaa", add(x, y, "aaa")
, "bbb", add(x, y, "bbb")
, "ccc", add(x, y, "ccc"));*/
}
)
);
}
public static int add (Map<String, Object> x, Map<String, Object> y, String key) {
return (int) x.get(key) + (int) y.get(key);
}
public static Map<String, Object> ofMap (Object... objs) {
System.out.println("ofMap");
Map<String, Object> map = new LinkedHashMap<>();
for (int i = 0; i < objs.length; i = i + 2) {
map.put(objs[i].toString(), objs[i + 1]);
}
return map;
}
public static List<Map<String, Object>> getDatas () {
List<Map<String, Object>> list = new ArrayList<>();
list.add(ofMap("name", "张三", "aaa", 3, "bbb", 5, "ccc", 6));
list.add(ofMap("name", "张三", "aaa", 8, "bbb", 51, "ccc", 521));
list.add(ofMap("name", "李四", "aaa", 9, "bbb", 53, "ccc", 23));
return list;
}
Stream分组求和使用笔记
话不多说,直接贴代码,分组使用
class Foo {
private int code;
private int count;
public Foo(int code, int count) {
this.code = code;
this.count = count;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public static void main(String[] args) {
Foo foo1 = new Foo(1, 2);
Foo foo2 = new Foo(2, 23);
Foo foo3 = new Foo(2, 6);
List<Foo> list = new ArrayList<>(4);
list.add(foo1);
list.add(foo2);
list.add(foo3);
Map<Integer, List<Foo>> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode));
List<Foo> list1 = collect.get(1);
List<Foo> list2 = collect.get(2);
list1.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount()));
System.out.println("-----------这里是分界线-----------------------------");
list2.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount()));
}
输出结果:
1:2
-----------这里是分界线-----------------------------
2:23
2:6
分组求和使用
public static void main(String[] args) {
Foo foo1 = new Foo(1, 2);
Foo foo2 = new Foo(2, 23);
Foo foo3 = new Foo(2, 6);
List<Foo> list = new ArrayList<>(4);
list.add(foo1);
list.add(foo2);
list.add(foo3);
Map<Integer, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode, Collectors.summarizingInt(Foo::getCount)));
IntSummaryStatistics statistics1 = collect.get(1);
IntSummaryStatistics statistics2 = collect.get(2);
System.out.println(statistics1.getSum());
System.out.println(statistics1.getAverage());
System.out.println(statistics1.getMax());
System.out.println(statistics1.getMin());
System.out.println(statistics1.getCount());
System.out.println(statistics2.getSum());
System.out.println(statistics2.getAverage());
System.out.println(statistics2.getMax());
System.out.println(statistics2.getMin());
System.out.println(statistics2.getCount());
}
输出结果:
2
2.0
2
2
1
29
14.5
23
6
2
stream真的是相当的好用,Mark一下,欢迎大神在评论区留下你的Stream骚操作。
来源:https://blog.csdn.net/weixin_42041388/article/details/119184542
0
投稿
猜你喜欢
- 最开始接触到相关的内容应该是从volatile关键字开始的吧,知道它可以保证变量的可见性,而且利用它可以实现读与写的原子操作。。。但是要实现
- MyBaties的基本配置标签1-全局配置文件(xxx.properties)引入的两种方式resource:引入类路径下的资源url:引入
- 如今代码圈很多做网络爬虫的例子,今天小编给大家分享的是如何用C#做网络爬虫。注意这次的分享只是分享思路,并不是一整个例子,因为如果要讲解一整
- 在开始本文前先考虑以下一个问题在不使用任何带有自动补全功能IDE的情况下,如何获取一个数组的长度?以及,如何获取一个字符串的长度?这个问题我
- 需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,如:手动重启、程序升
- 一、简介当我们没有在子类构造函数中写上 base(),默认会先调用父类中无参的构造函数,再调用子类。当在有参构造函数后写上base时,只调用
- Note:这篇文章是基于Android Studio 3.01版本的,NDK是R16。step1:创建一个包含C++的项目其他默认就可以了。
- 我就废话不多说了,大家还是直接看代码吧~ public List<FreightM> sortList(List&l
- 1. 什么是 spring-boot-devtoolsspring-boot-devtools 是 spring-boot项目开发时的一个热
- 1、public String(char[] c,begin,length).从字符数组c的下标begin处开始,将长度为length的字符
- @RequestBody和@ResponseBody及Stringify()的作用首先,在项目的前后交互中json数据格式比较常用,普遍认为
- 本文实例讲述了Java获取时间年、月、日的方法。分享给大家供大家参考。具体实现方法如下:package com.date.demo; imp
- 先利用jsoup将得到的html代码“标准化”(Jsoup.parse(String html))方法,然后利用FileWiter将此htm
- 本文实例讲述了C#.net实现在Winform中从internet下载文件的方法。分享给大家供大家参考。具体如下:自己做了一个通用的软件自动
- 要实现摇一摇的功能,类似于微信的摇一摇方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂)方法2:iOS自带的Shake监控AP
- 插入排序原理①把所有元素分成已排序和未排序两组②找到未排序组的第一个元素,向已经排序的组中进行插入③倒序遍历已经排好的元素,依次和待插入的元
- 第一部分: 使用idea 打包工程jar 1.准备好一份 开发好的 可执行的 含有main方法的&nbs
- 实际项目中pom.xml依赖写法: <dependency> <groupId>org.springf
- 实现了Java web开发账号单一登录的功能,防止同一账号重复登录,后面登录的踢掉前面登录的,使用过滤器Filter实现的。可以先下载项目下
- 本实例实现在jsp页面实现查询全国城市天气预报的功能,供大家参考,具体内容如下实例目录:实现效果:具体思路:从和风天气api那里取得具体城市