Using Collectors to GroupBy one field, Count and Add another field value

CleverDev :

I would like to use Collectors in order to groupBy one field, count and add the value of another field. For example:

This is my DB Table:

id    host    result    numberof    date
23    host1   24GB         1        2019-05-20
23    host7   10GB         1        2019-04-21
23    host3   24GB         3        2019-05-12
23    host4   10GB         1        2019-04-22

What I want to do, is to groupBy result column, then count BUT also take into account the value of 'numberof'. so In this case after grouping, counting and adding the values:

"24GB": 4
"10GB": 2

Not really sure how to do it, I hope you guys can help me out.

Map<String,Long> statusMap = statusList.stream()
            .collect(Collectors.groupingBy(
                Status::getResult,
                Collectors.counting()));
Eugene :
 Map<String,Long> statusMap = statusList.stream()
        .collect(Collectors.groupingBy(
            Status::getResult,
            Collectors.summingLong(Status::getNumberOf)));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100396&siteId=1