【发布时间】:2020-12-03 10:41:10
【问题描述】:
我需要统计一些特殊捆绑包的出现次数。
Map<Integer,Integer> countBundles(){
return bundles.stream()
.bla()
.bla()
.bla()
.collect(groupingBy(Bundle::getId), counting()));
}
此代码无法编译,因为计数返回 Long。
有什么漂亮的方法可以返回 Map
我有这个想法,但是很丑
map.entrySet().stream()
.collect(toMap(Map.Entry::getKey, entry -> (int) entry.getValue().longValue()));
【问题讨论】:
-
应该没有其他方法可以使用
StreamsAPI。除此之外,我不同意您到目前为止所尝试的内容是丑陋的,因为这看起来像是遵循功能类型的明显方法。 -
此链接对您有帮助吗? stackoverflow.com/questions/51968025/…
标签: java java-stream collectors