Java 8 stream 分组 合并

使用很简单,功能很强大

package com.lee;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {

        List<Buss> bussList = new ArrayList<>();
        bussList.add(new Buss("a",10,0.3));
        bussList.add(new Buss("b",3,0.8));
        bussList.add(new Buss("c",5,2.0));
        bussList.add(new Buss("b",30,3.2));
        bussList.add(new Buss("c",20,0.1));

        List<Buss> st = new ArrayList<>();
        bussList.stream()
                .collect(Collectors.groupingBy(Buss::getName)) //分组(Name can't be null)
                .forEach((k,v) -> {
                    Optional<Buss> sum = v.stream().reduce((v1,v2) -> {  //合并
                        v1.setCount(v1.getCount()+v2.getCount());
                        v1.setValue(v1.getValue()+v2.getValue());
                        return v1;
                    });
                    st.add(sum.orElse(new Buss("other",0,0.0)));

                });
        System.out.println(st);

    }
}

class Buss {

    private String name;
    private int count;
    private double value;

    public Buss(String name, int count, double value) {
        this.name = name;
        this.count = count;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Buss{" +
                "name='" + name + '\'' +
                ", count=" + count +
                ", value=" + value +
                '}';
    }
}

Output

[Buss{name='a', count=10, value=0.3}, Buss{name='b', count=33, value=4.0}, Buss{name='c', count=25, value=2.1}]

如何根据两个字段排序
可以再 model里面 ,写一个类似的方法即可
下例,分组的key 就是 name+value

public String groupField() {
        return getName()+"-"+getValue();
    }
    原文作者:LeeZhao
    原文地址: https://blog.csdn.net/u010468602/article/details/78107200
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞