【发布时间】:2020-08-29 09:14:29
【问题描述】:
我有班级国家:
class Country {
private String code;
private String name;
}
我需要从国家列表到带有代码和名称的地图。 我试过了
Map<String, String> countryNames = countries.stream()
.collect(Collectors.groupingBy(Country::getCode, Country::getName));
但这是不对的。如何正确收集?
【问题讨论】:
-
countries.stream().collect(Collectors.toMap(Country::getCode, Country::getName)); -
你要使用Collectors.toMap
-
它不起作用 UPD:没关系,我的错误,谢谢
标签: java java-stream