Java stream filter sum of values

Viewed 5759

I have a class called MonitoredData with which describes an activity, its starting time and ending time. The attributes are activityLabel, startTime, endTime. I have to group and filter these activities, using streams, the activities which have the total duration of more than 10 hours. I managed to make the sum of durations and group them according to the activity using this:

Map<String, Long> map4 = new HashMap<String, Long>();

map4 = data.stream()
    .collect(
        Collectors.groupingBy(
            MonitoredData::getActivity, 
            Collectors.summingLong(MonitoredData::getDuration)
        )
    ); //getDuration returns end Time - startTime in milliseconds

But I haven't managed to add a filter. I tried using:

.filter(Collectors.summingLong(MonitoredData::getDuration) > whatever)

but obviously it doesn't work. How can I solve this in order to make it return a Map<String, Long>?

4 Answers
Related