Total aggregate attribute of a Stream object

I have a list of objects sorted by String attribute of the month. My defination class object looks like

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)

I want to make a cumulative amount of at membercount, nonmembercount, memberpayment,nonmemberpayment

So my new list of objects will look below

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)

I tried with Collectors.summingDouble, but it gives me the whole amount, not cumulative.

Really appreciate any pointers.

+4
source share
3 answers

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)

, , . , ...


, . 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
)));
+4

, , .

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;
    }

}

, !

0

My decision:

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

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

Source: https://habr.com/ru/post/1669240/


All Articles