Java8 Stream流多字段求和、汇聚的实例
作者:我想写游戏 时间:2023-03-14 20:17:23
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
标签:Java8,Stream,字段求和
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文看懂RabbitMQ消息丢失如何防止
2022-03-03 20:36:24
![](https://img.aspxhome.com/file/2023/9/64139_0s.jpg)
Android中Listview点击item不变颜色及设置listselector 无效的解决方案
2022-07-04 23:49:10
AndroidStudio替换项目图标ic_launcher操作
2023-03-20 09:48:07
![](https://img.aspxhome.com/file/2023/1/122061_0s.jpg)
Android中通过view方式获取当前Activity的屏幕截图实现方法
2021-09-20 09:17:22
C#调用QQ_Mail发送邮件实例代码两例
2023-11-10 17:27:05
解析MapStruct转换javaBean时出现的诡异事件
2022-05-15 01:16:28
![](https://img.aspxhome.com/file/2023/1/71701_0s.png)
Android自定义ProgressDialog进度等待框
2022-07-09 13:51:46
![](https://img.aspxhome.com/file/2023/0/95000_0s.png)
c#中的浮点型转整形的舍取 四舍五入和银行家舍入实现代码
2023-01-10 02:04:41
![](https://img.aspxhome.com/file/2023/1/113831_0s.jpg)
Flutter有状态组件StatefulWidget生命周期详解
2023-09-25 23:56:50
![](https://img.aspxhome.com/file/2023/3/137373_0s.png)
JAVA发送HTTP请求的多种方式详细总结
2021-06-15 14:43:48
Java内存区域管理详解
2023-11-10 23:44:42
![](https://img.aspxhome.com/file/2023/3/59173_0s.png)
Android SharedPreferences存储用法详解
2023-08-07 08:25:34
![](https://img.aspxhome.com/file/2023/8/83528_0s.gif)
java中断线程的正确姿势完整示例
2023-01-27 06:33:58
![](https://img.aspxhome.com/file/2023/8/67648_0s.jpg)
Android Monkey压力测试详细介绍
2021-10-24 08:02:37
![](https://img.aspxhome.com/file/2023/0/137790_0s.png)
springMvc请求的跳转和传值的方法
2023-02-06 17:29:56
C#实现组合排列的方法
2021-10-04 02:25:01
![](https://img.aspxhome.com/file/2023/9/111229_0s.jpg)
带你了解Java中Static关键字的用法
2021-11-07 15:04:32
![](https://img.aspxhome.com/file/2023/8/66038_0s.png)
常用的C语言排序算法(两种)
2022-12-28 02:28:37
IntelliJ IDEA安装插件阿里巴巴Java开发手册(Alibaba Java Coding Guidelines)
2023-10-17 11:30:02
![](https://img.aspxhome.com/file/2023/3/113443_0s.png)
android上的一个网络接口和图片缓存框架enif简析
2022-10-06 07:37:56
![](https://img.aspxhome.com/file/2023/2/117162_0s.png)