简体   繁体   中英

How can i use lambda method summaryStatistics() to find statistics of LocalTime values?

I have a list of maps with simple Runner.class entities:

List<Map<String, Runner>> times

, for example:

{
   "pedro": [
      { lap=1, time=00:01:16.11 },
      { lap=2, time=00:00:42.45 },
      { lap=3, time=00:00:58.23 }
   ],
   "huan": [
      { lap=1, time=00:00:48.33 },
      { lap=2, time=00:00:41.21 }
   ]
   "gomez": [
      { lap=1, time=00:01:02.42 },
      { lap=2, time=00:01:12.31 },
      { lap=3, time=00:01:58.14 },
      { lap=3, time=00:00:55.41 }
   ]
}

Here is my runner class:

public class Runner {

   private Integer lap;
   private LocalTime time;
   // Getters and setters...
}

I need to get a map of statistics, like lambda summaryStatistics() does, of min, max and avg time for each runner, that looks like this:

Map<String, DoubleSummaryStatistics> statsMap

, so i could iterate it and use getMin() , getAverage() and getMax() methods... But I don't have any clue how to deal with LocalTime?

One of the ways to do the transformation as suggested in comments could be as follows:

public Map<String, DoubleSummaryStatistics> createStats(Map<String, List<Runner>> times) {
    return times.entrySet().stream()
          .collect(Collectors.toMap(Map.Entry::getKey,
                  e ->e.getValue().stream()
                          .map(Runner::getTime)
                          .mapToDouble(LocalTime::toSecondOfDay) // inverse while extrating summary details
                          .summaryStatistics()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM