累积和多个对象属性Stream

时间:2017-02-09 07:45:14

标签: java java-8 java-stream

我有一个按月 - 年字符串属性排序的对象列表。 我的对象类定义看起来像

Public class Obj{
    String year;
    Long membercount;
    Long nonmembercount;
    Double memberpayment;
    Double nonmemberpayment;
}

new Obj("9-2015",100,20,10,5)
new Obj("10-2015",220,40,20,55)
new Obj("11-2015",300,60,30,45)
new Obj("12-2015",330,30,50,6)
new Obj("1-2016",100,10,10,4)

我想在membercountnonmembercountmemberpaymentnonmemberpayment

上累积总和

所以我的新对象列表如下所示

new Obj("9-2015",100,20,10,5)
new Obj("10-2015",320,60,30,60)
new Obj("11-2015",620,120,60,105)
new Obj("12-2015",950,150,110,111)
new Obj("1-2016",1050,160,120,115)

我尝试使用Collectors.summingDouble,但它给了我所有不累积的金额。

非常感谢任何指示。

3 个答案:

答案 0 :(得分:4)

Stream API中没有直接支持累积操作,但可以通过自定义Collector实现此类操作。但值得注意的是,对阵列上的此类操作已有直接支持,这可能足以满足您的需求:

Obj的草图扩展为

public class Obj {
    String year;
    Long membercount;
    Long nonmembercount;
    Double memberpayment;
    Double nonmemberpayment;

    public Obj(String year, long membercount, long nonmembercount,
            double memberpayment, double nonmemberpayment) {
        this.year = year;
        this.membercount = membercount;
        this.nonmembercount = nonmembercount;
        this.memberpayment = memberpayment;
        this.nonmemberpayment = nonmemberpayment;
    }

    @Override
    public String toString() {
        return "Obj("+year+", "+membercount+", "+nonmembercount
            +", "+memberpayment+", "+nonmemberpayment+')';
    }
}

解决方案可能如下所示:

// test data
List<Obj> list=Arrays.asList(
    new Obj("9-2015", 100, 20, 10, 5),
    new Obj("10-2015", 220, 40, 20, 55),
    new Obj("11-2015", 300, 60, 30, 45),
    new Obj("12-2015", 330, 30, 50, 6),
    new Obj("1-2016", 100, 10, 10, 4));

// creating an array as need for the operation, it will contain the
// result afterwards, whereas the source list is not modified
Obj[] array = list.toArray(new Obj[0]);

// the actual operation
Arrays.parallelPrefix(array, (a,b) -> new Obj(b.year,
    a.membercount + b.membercount,
    a.nonmembercount + b.nonmembercount,
    a.memberpayment + b.memberpayment,
    a.nonmemberpayment + b.nonmemberpayment
));

// just print the result
Arrays.asList(array).forEach(System.out::println);

最后一行将打印

Obj(9-2015, 100, 20, 10.0, 5.0)
Obj(10-2015, 320, 60, 30.0, 60.0)
Obj(11-2015, 620, 120, 60.0, 105.0)
Obj(12-2015, 950, 150, 110.0, 111.0)
Obj(1-2016, 1050, 160, 120.0, 115.0)

虽然这个操作不太可能从这个少量元素的并行处理中受益,但遗憾的是没有这个操作的顺序版本。所以你可以考虑使用普通的循环解决方案......

为了完整性,这里有一个基于Stream Collector的累积操作解决方案。与Arrays.parallelPrefix一样,更新函数必须是无副作用和关联的,这是函数返回具有总结属性的新对象的情况。

public static <T> Collector<T,?,List<T>> cumulative(BinaryOperator<T> update) {
    return Collector.of(ArrayList::new,
        (l,o) -> {
            if(!l.isEmpty()) o=update.apply(l.get(l.size()-1), o);
            l.add(o);
        },
        (l,m) -> {
            if(l.isEmpty()) return m;
            if(!m.isEmpty()) {
                T a = l.get(l.size()-1);
                for(T b: m) l.add(update.apply(a, b));
            }
            return l;
        });
}

使用上述设置:

List<Obj> result = list.stream().collect(cumulative((a,b) -> new Obj(b.year,
    a.membercount + b.membercount,
    a.nonmembercount + b.nonmembercount,
    a.memberpayment + b.memberpayment,
    a.nonmemberpayment + b.nonmemberpayment
)));

答案 1 :(得分:0)

根据我对你的问题的理解,这是我能够提出的。

我使用map(o->o.membercount)collect(Collectors.summingDouble(n->n))为每个字段进行计算。看一看。

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

public class DoubleSummation {

    public static void main(String[] args) {
        List<Obj> list = new ArrayList<>();
        list.add(new Obj("9-2015",100,20,10,5));
        list.add(new Obj("10-2015",220,40,20,55));
        list.add(new Obj("11-2015",300,60,30,45));
        list.add(new Obj("12-2015",330,30,50,6));
        list.add(new Obj("1-2016",100,10,10,4));

        Double sumMemberCount = list.stream().mapToDouble(o->o.membercount).sum();
        Double sumNonmembercount = list.stream().mapToDouble(o->o.nonmembercount).sum();
        Double sumMemberpayment = list.stream().mapToDouble(o->o.memberpayment).sum();
        Double sumNonmemberpayment = list.stream().mapToDouble(o->o.nonmemberpayment).sum();

        System.out.println(sumMemberCount);
        System.out.println(sumNonmembercount);
        System.out.println(sumMemberpayment);
        System.out.println(sumNonmemberpayment);
    }
}

class Obj{

    String year;
    long membercount;
    long nonmembercount;
    double memberpayment;
    double nonmemberpayment;

    public Obj(String year, long membercount, long nonmembercount, double memberpayment, double nonmemberpayment) {
        this.year = year;
        this.membercount = membercount;
        this.nonmembercount = nonmembercount;
        this.memberpayment = memberpayment;
        this.nonmemberpayment = nonmemberpayment;
    }

}

希望这有帮助!

答案 2 :(得分:0)

我的解决方案:

List<Obj> objects = ...;
int cumulSum[] = {0};

objects.stream().map(obj -> {
    cumulSum[0] += obj.membercount;
    return new Obj(obj.year, cumulSum[0], ...);
});