简体   繁体   English

如何使用 lambda 方法 summaryStatistics() 来查找 LocalTime 值的统计信息?

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

I have a list of maps with simple Runner.class entities:我有一个简单的 Runner.class 实体的地图列表:

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:这是我的跑步者 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 的统计数据,就像 lambda summaryStatistics()一样,每个跑步者的最小、最大和平均时间,看起来像这样:

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? ,所以我可以迭代它并使用getMin()getAverage()getMax()方法......但我不知道如何处理 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()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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