我有这样的地图设置:
Map<Integer, Set<Long>> myMap = new HashMap<Integer, Set<Long>>();
Run Code Online (Sandbox Code Playgroud)
我正在尝试将我的第一个值添加到myMap这样:
myMap.put(1, myMap.get(1).add(myLong));
Run Code Online (Sandbox Code Playgroud)
而java返回这个:
The method put(Integer, Set<Long>) in the type Map<Integer, Set<Long>> is not applicable for the arguments (int, boolean)
Set.add返回一个布尔值,指示该集是否已更改.将您的代码更改为:
myMap.get(1).add(myLong);
Run Code Online (Sandbox Code Playgroud)
(只要你知道myMap.get(1)已经存在).如果myMap.get(1)可能尚不存在,那么你需要做这样的事情:
Set<Long> set = myMap.get(1);
if (set == null) {
set = new HashSet<Long>(); // or whatever Set implementation you use
myMap.put(1, set);
}
set.add(myLong);
Run Code Online (Sandbox Code Playgroud)