0

consider the following class:

public class PersonalExpense {
    private String name;
    private int currentExpenses;

    ...

how to can i write a java stream "pipeline" that works in the same way as the first method (getRemainingCompanyBalance). this code results in an error.

the list contains separate lists for each department. each entry in the sub-list is an instance of the class. name/cost:

de#1first"/9900, de#1second"/8700, de#2first"/8500, de#2second"/10000, de#3first"/7800, de#3second"/6900

    private static long getRemainingCompanyBalance ( long initialBalance, List<ArrayList<PersonalExpense>> total) {
        long remainingBalance = initialBalance;
        for (List<PersonalExpense> departmentExpense : total) {
            for (PersonalExpense personalExpense : departmentExpense) {
                System.out.println(personalExpense.getName());
                remainingBalance = remainingBalance - personalExpense.getCurrentExpenses();
            }
        }
        return remainingBalance;
    }

    public static long getRemainingCompanyBalanceLambda ( long initialBalance, List<ArrayList<PersonalExpense>> total) {
        long remainingBalance = initialBalance;


        Integer sum = total
        .stream()
        .flatMap(Collection::stream)
        .filter(pe -> pe instanceof PersonalExpense)
        .map (pe -> (PersonalExpense) pe)
        .collect(Collectors.toList())
        .mapToInt(PersonalExpense::getCurrentExpenses)
        .sum();


        return remainingBalance -= sum;
    }

}

i'm trying to collect costs, then subtract them from the balance

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
  • hmm I personally don't see this as a duplicate, it's not that plain simple as computing the `sum`, specially since the OP seems to not know what `flatMap` and the like... – Eugene Sep 25 '18 at 13:44

3 Answers3

1
public static long getRemainingCompanyBalanceLambda ( long initialBalance, List<ArrayList<PersonalExpense>> total) {

   int sum = total.stream()
        .flatMap(List::stream)
        .mapToInt(PersonalExpense::getCurrentExpenses)
        .sum();

   return initialBalance - sum;

}
Eugene
  • 117,005
  • 15
  • 201
  • 306
0

You got some unnecessary method calls. You don't need to filter the Stream, since it already contains just PersonalExpense instances, and you shouldn't collect it to a List, because that would prevent you from mapping it to an IntStream and computing the sum.

public static long getRemainingCompanyBalanceLambda ( long initialBalance, List<ArrayList<PersonalExpense>> total) {
    return initialBalance - total
    .stream()
    .flatMap(Collection::stream)
    .mapToInt(PersonalExpense::getCurrentExpenses)
    .sum();
}
Eran
  • 387,369
  • 54
  • 702
  • 768
0

With mapToInt function

int sum = total.stream()
         .mapToInt(PersonalExpense::getCurrentExpenses)
         .sum();

With summingInt function

int sum = total.stream()
         .collect(Collectors.summingInt(PersonalExpense::getCurrentExpenses));
burak isik
  • 395
  • 5
  • 6