Java 8 collect, count with sum

Viewed 6258

I have a data in the following format:

ProductName | Date
------------|------
ABC         | 1-May
ABC         | 1-May
XYZ         | 1-May
ABC         | 2-May

It is in the form of List, where Product consists of ProductName and Date. Now I wanted to group these data and get the count with sum as follows:

1-May
  -> ABC : 2
  -> XYZ : 1
  -> Total : 3
2-May
  -> ABC: 1
  -> Total : 1

So far what I've achieved is grouping with counting, but not the total value.

myProductList.stream()
  .collect(Collectors.groupingBy(Product::getDate, 
            Collectors.groupingBy(Product::getProductName, Collectors.counting())));

Not sure how do I get the total value.

4 Answers
Related