Nick T
Nick T

Reputation: 83

How to aggregate multiple fields using Collectors in Java

I have a custom Object Itemized which has two fields amount and tax. I have an array of Itemized objects and I am looking to sum the two fields in the same stream. Below is how I am calculating the sum of both the fields.

double totalAmount = Arrays.stream(getCharges()).map(Itemized::getAmount).reduce(0.0, Double::sum));
double totalTax = Arrays.stream(getCharges()).map(Itemized::getTax).reduce(0.0, Double::sum));

Is there any way I don't have to parse the stream two times and can sum the two fields in one go ? I am not looking to sum totalTax and totalAmount but want their sum separately. I was looking at Collectors but was not able to find any example which would allow aggregating of multiple fields in one go.

Upvotes: 6

Views: 1579

Answers (6)

NimChimpsky
NimChimpsky

Reputation: 47300

use a for loop ?

double taxSum = 0;
double amountSum = 0;
for (Itemized itemized : getCharges()) {
    taxSum += itemized.getTax();
    amountSum += itemized.getAmount();
}

Upvotes: 4

Leonel
Leonel

Reputation: 1

In your specific case, it's could be done by using your Itemized class as value holder.

Itemized result = Arrays.stream(getCharges())
    .reduce(new Itemized(), (acc, item) -> {
      acc.setAmount(acc.getAmount() + item.getAmount());
      acc.setTax(acc.getTax() + item.getTax());
      return acc;
    });
double totalAmount = result.getAmount();
double totalTax = result.getTax();

Upvotes: 0

Avi
Avi

Reputation: 2641

You can try to use the teeing Collector, like so:

Arrays.stream(getCharges())                                // Get the charges as a stream
    .collect(Collectors                                    // collect
        .teeing(                                           // both of the following:
            Collectors.summingDouble(Itemized::getAmount), //     first, the amounts
            Collectors.summingDouble(Itemized::getTax),    //     second, the sums
            Map::entry                                     // and combine them as an Entry
        )
    );

This should give you a Map.Entry<Double, Double> with the sum of amounts as the key and the sum of tax as the value, which you can extract.

Edit:
Tested and compiled it - it works. Here we go:

ItemizedTest.java

public class ItemizedTest
{
    static Itemized[] getCharges()
    {
        // sums should be first param = 30.6, second param = 75
        return new Itemized[] { new Itemized(10, 20), new Itemized(10.4,22), new Itemized(10.2, 33) };
    }

    public static void main(String[] args)
    {
        Map.Entry<Double, Double> sums = Arrays.stream(getCharges())
        .collect(Collectors
            .teeing(
                Collectors.summingDouble(Itemized::getAmount), 
                Collectors.summingDouble(Itemized::getTax),
                Map::entry
            )
        );
        System.out.println("sum of amounts: "+sums.getKey());
        System.out.println("sum of tax: "+sums.getValue());
    }
}

Itemized.java

public final class Itemized
{
    final double amount;
    final double tax;

    public double getAmount()
    {
        return amount;
    }

    public double getTax()
    {
        return tax;
    }

    public Itemized(double amount, double tax)
    {
        super();
        this.amount = amount;
        this.tax = tax;
    }
}

Output:

sum of amounts: 30.6
sum of tax: 75.0

P.S.: teeing Collector is only available in Java 12+.

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 40078

You can do it by using Entry but still you will end up in creating lot of objects, the best solution i would suggest is for loop answered by NimChimpsky

Entry<Double, Double> entry = Arrays.stream(new Itemized[] {i1,i2})
          .map(it->new AbstractMap.SimpleEntry<>(it.getAmount(), it.getTax()))
          .reduce(new AbstractMap.SimpleEntry<>(0.0,0.0),
                  (a,b)->new AbstractMap.SimpleEntry<>(a.getKey()+b.getKey(),a.getValue()+b.getValue()));

    System.out.println("Amount : "+entry.getKey());
    System.out.println("Tax : "+entry.getValue());

Upvotes: 0

ernest_k
ernest_k

Reputation: 45339

With a data structure that can allow one to accumulate both sums, you can reduce the stream to a single object.

This is using double[] to hold totalAmount at index 0 and totalTax at index 1 (other options include SimpleEntry, Pair):

double[] res = Arrays.stream(getCharges())
                .map(ch -> new double[] { ch.getAmount(), ch.getTax() })
                .reduce(new double[] { 0, 0 }, 
                        (a1, a2) -> new double[] { a1[0] + a2[0], a1[1] + a2[1] });

double totalAmount = res[0], 
       totalTax = res[1];

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Instead of summing by field, you define a custom object to hold both field's sum values:

ItemizedValues {
    private double amount;
    private double tax;

    public static final ItemizedValues EMPTY = new ItemizedValues(0, 0);

    // Constructor - getter - setter
    public static ItemizedValues of(Itemized item) {
         return new ItemizedValues(amount, tax);
    }

    public static ItemizedValues sum(ItemizedValues a, ItemizedValues b) {
         // Sum the fields respectively
         // It's your choice to reuse the existing instances, modify the values or create a brand new one
    }
}

Arrays.stream(getCharges())
      .map(ItemizedValues::of)
      .reduce(ItemizedValues.EMPTY, ItemizedValues::sum);

Upvotes: 0

Related Questions