Java流返回地图
如何使用Java流解决以下问题?
给定: -
class Dress {
private String brandName;
private String color;
public String getBrandName() {
return brandName;
}
public String getColor() {
return color;
}
}
List<Dress> dresses = getDresses(); // API call
必需: -
// Number of dresses per color
Map<String, Integer> colorToCountMap;
How can I get the following problem solved using Java Streams?
Given:-
class Dress {
private String brandName;
private String color;
public String getBrandName() {
return brandName;
}
public String getColor() {
return color;
}
}
List<Dress> dresses = getDresses(); // API call
Required:-
// Number of dresses per color
Map<String, Integer> colorToCountMap;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以使用
长
而不是整数
,则可以使用collect> collector> collectors.countors.countors.counting()
作为下游收集器:如果它必须是
整数
,您可以使用collector.summingint(x - &gt; 1)
,如注释中所述:If you can live with
Long
instead ofInteger
, you can useCollectors.counting()
as downstream collector:If it has to be
Integer
, you can useCollectors.summingInt(x -> 1)
as mentioned in the comments:您可以使用以下代码获取
map&lt; string,Integer&gt;
地图You can get
Map<String, Integer>
map using following code