4å¹´åã«æ¸ãããªã¹ããé ç®ãã¨ã«éè¨ããã¨ããã¨ã³ããªãè¦ã¤ããã®ã§ããªãã¨ãªãä»é¢¨ã«Stream使ã£ã¦æ¸ãã¦ã¿ãã
Collection<Output> summary(List<Input> data) { return data -> { Map<String, List<Input>> collected = data.stream() .collect(groupingBy(Input::getKey)); return collected.entrySet().stream() .map(e -> new Output( e.getKey(), e.getValue().stream().mapToInt(Input::getValue).sum())) .collect(toList()); }; }
â¦â¦ãã¼ãã
åã«Map<String, Integer>
ã«ããã ããªãã¾ã è¦ãããã ãã©ã
Map<String, Integer> summary(List<Input>
return data.stream().collect(
groupingBy(Input::getKey, summingInt(Input::getValue)));
}
å®è£ åä¸è¶³ããããã ãã©ãä½ã§ãããã§ãStreamã«ãããããããã§ã¯ãªããªã¼ã¨æ¹ãã¦ã ãã¶ãIntSummaryStatisticsã¿ãããªéè¨ãªãã¸ã§ã¯ãä½ã£ãä¸ã§ãã£ãã»ããè¯ãããã«æãã¾ããã
ä½éããæ¸ãã¦ã¿ããã©ã¤ãã¤ãã ã¯ã¼ã