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


猜你喜欢
- Vector(向量)是 java.util 包中的一个类,该类实现了类似动态数组的功能。向量和数组相似,都可以保存一组数据(数据列表)。但是
- 效果图:A.绘制圆环,圆弧,文本//1.画圆环//原点坐标float circleX = width / 2;float circleY =
- 基本布局演示1. 定义包含GridView 的 main.xmk<?xml version="1.0" encod
- 1.使用response对象提供的sendRedirect()方法可以将网页重定向到另一个页面。重定向操作支持将地址重定向到不同的主机上,这
- 一、Statementimport java.sql.*;public class TestJDBC { public stati
- 问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException分析:这种异常返回的原因,
- 本文实例讲述了C++实现的O(n)复杂度内查找第K大数算法。分享给大家供大家参考,具体如下:题目:是在一组数组(数组元素为整数,可正可负可为
- 一、项目简述功能: 区分为管理员用户和普通用户,普通用户:用户登录,个 人信息修改,图书查询,用户借阅,用户归还,管理员用 户:图书馆里,归
- 常需要对list进行排序,小到List<String>,大到对自定义的类进行排序。不需要自行归并或堆排序。简单实现一个接口即可。
- 在开发中我们经常需要把我们的应用设置为全屏,有两种方法,一中是在代码中设置,另一种方法是在配置文件里改!一、在代码中设置:package c
- 本文实例讲述了Android控件之Spinner用法。分享给大家供大家参考。具体如下:以下模拟下拉列表的用法布局文件:<?xml ve
- 需求描述:企业开发过程中,经常需要将一些静态文本数据放到Resources目录下,项目启动时或者程序运行
- 给新建的winform程序添加资源文件夹Resources小菜鸟开始学习WinForm程序别人的项目都有资源文件夹放图片之类的,我的就是没有
- 一、layui.use1、LayUI的官方使用文档:https://www.layui.com/doc/2、layui的内置模块不是默认就加
- 方式一public class Test{ public static void main(String[] args) throws Ex
- 1、泛型的基础概念1.1 为什么需要泛型 List list = new ArrayList();//默认类型是Object
- 本文实例为大家分享了java实现小球碰撞的具体代码,供大家参考,具体内容如下这次我们做一个小球的碰撞的游戏,规则是:按下添加按钮,窗口的中心
- 前言目前有两套RocketMQ集群,集群A包含topic名称为cluster_A_topic,集群B包含topic名称为cluster_B_
- /** * 实现 * @author dujinyang * */顺序是: OneAcitivity
- class文件中的attributes_count和attributesattributes_count位于class文件中methods的