服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java8 Stream流多字段求和、汇聚的实例

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

2022-12-02 17:34我想写游戏 Java教程

这篇文章主要介绍了Java8 Stream流多字段求和、汇聚的实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Stream流多字段求和、汇聚

实现方法

利用

?
1
Collectors.toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)
  • keyMapper:代表你最终想要获得的Map<Key, Value> 的Key
  • valueMapper:代表你最终想要获得的Map<Key, Value> 的Value
  • mergeFunction:表示碰到Key冲突是处理过程,{x, y}中x是已汇聚对象,y表示当前处理对象

对象类型数据处理

?
1
2
3
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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类型数据处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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分组求和使用笔记

话不多说,直接贴代码,分组使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

分组求和使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

延伸 · 阅读

精彩推荐
  • Java教程SpringBoot配置Email发送功能实例

    SpringBoot配置Email发送功能实例

    本篇文章主要介绍了SpringBoot配置Email发送功能实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    qianweifeng1233892020-09-19
  • Java教程java基于ConcurrentHashMap设计细粒度实现代码

    java基于ConcurrentHashMap设计细粒度实现代码

    这篇文章主要介绍了java基于ConcurrentHashMap设计细粒度实现代码,通过ConcurrentHashMap实现细粒度,具有一定参考价值,需要的朋友可以了解。...

    犀利java6312021-01-18
  • Java教程SpringBoot集成swagger的实例代码

    SpringBoot集成swagger的实例代码

    Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。这篇文章主要介绍了SpringBoot集成swagger,需要的朋友可以参考下...

    Smilence^^8582021-03-02
  • Java教程MyBatis中如何接收String类型的参数实现

    MyBatis中如何接收String类型的参数实现

    这篇文章主要介绍了MyBatis中如何接收String类型的参数实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    完美主义编程小白4452021-08-03
  • Java教程Spring Security学习之remember Me自动登录的实现

    Spring Security学习之remember Me自动登录的实现

    这篇文章主要给大家介绍了关于Spring Security学习之remember Me自动登录的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    西红柿鸡蛋面~4562020-06-28
  • Java教程实例讲解spring boot 多线程

    实例讲解spring boot 多线程

    这篇文章主要介绍了spring boot 多线程的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下 ...

    gdwkong1742020-07-05
  • Java教程java短网址服务(TinyURL)生成算法

    java短网址服务(TinyURL)生成算法

    这篇文章主要为大家详细介绍了java短网址服务生成算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Java教程网11602021-05-27
  • Java教程Java中指定时区的3种方法

    Java中指定时区的3种方法

    这篇文章主要介绍了Java中指定时区的3种方法,本文是一个JAVA项目和.NET项目通讯时遇到的问题,本文给出JAVA中的3种解决方法,需要的朋友可以参考下 ...

    junjie7292019-12-10