Java8 Stream流多字段求和、汇聚的实例

网友投稿 1106 2022-07-31


目录Stream流多字段求和、汇聚实现方法对象类型数据处理Map类型数据处理Stream分组求和使用笔记分组求和使用

Stream流多字段求和、汇聚

实现方法

利用

Collectors.toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)

keyMapper:代表你最终想要获得的Map 的KeyvalueMapper:代表你最终想要获得的Map 的ValuemergeFunction:表示碰到Key冲突是处理过程,{x, y}中x是已汇聚对象,y表示当前处理对象

对象类型数据处理

public static Map streamGroupSum(List 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 set, Function get, Model y){

set.accept(this, get.apply(this) + get.apply(y));

return this;

}

}

Map类型数据处理

public static void main (String[] args) {

List> datas = getDatas();

streamMapSum(datas);

}

public static Map> streamMapSum (List> 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 x, Map y, String key) {

return (int) x.get(key) + (int) y.get(key);

}

public static Map ofMap (Object... objs) {

System.out.println("ofMap");

Map 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> getDatas () {

List> 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 list = new ArrayList<>(4);

list.add(foo1);

list.add(foo2);

list.add(foo3);

Map> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode));

List list1 = collect.get(1);

List 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:232:6

分组求和使用

public sthttp://atic void main(String[] args) {

Foo foo1 = new Foo(1, 2);

Foo foo2 = new Foo(2, 23);

Foo foo3 = new Foo(2, 6);

List list = new ArrayList<>(4);

list.add(foo1);

list.add(foo2);

list.add(foo3);

Map 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());

}

输出结果:

22.02212914.52362

stream真的是相当的好用,Mark一下,欢迎大神在评论区留下你的Stream骚操作。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用AOP统一日志管理的方法详解(springboot aop日志管理)
下一篇:Java8 Stream流根据多个字段去重
相关文章

 发表评论

暂时没有评论,来抢沙发吧~