【发布时间】:2016-05-11 14:38:04
【问题描述】:
我想用Map<String, List<String>> 记录一些东西,例如
每个城市都有多少用户。
现在我的代码是
Map<String, List<String>> map = new HashMap<>();
if(map.get("city_1")==null){
map.put("city_1", new ArrayList<>());
}
map.get("city_1").add("aaa");
但我觉得有点麻烦,我想要这个效果
Map<String, List<String>> map = new HashMap<>();
map.compute("city_1", (k,v)->v==null?new ArrayList<>():v.add("aaa"));
但它有编译错误:
Type mismatch: cannot convert from boolean to List<String>
那么还有其他方法可以简化吗?
【问题讨论】:
标签: java-8