Qwe*_*rty -4 java dictionary hashmap set hashset
如何向 HashMap 内的 HashSet 添加值?
Map<String, Set<String>> myMap;
Run Code Online (Sandbox Code Playgroud)
get 集合null!put首先在地图中为空。Set<String> theSet = myMap.get(aKey);
if (theSet == null) {
theSet = new HashSet<String>();
myMap.put(aKey, theSet);
}
theSet.add(value);
Run Code Online (Sandbox Code Playgroud)
// ...
Map<String, Set<String>> myMap = new HashMap<String, Set<String>>();
addValue("myValue", "myKey", myMap);
// ...
private void addValue(String value, String key, Map<String, Set<String>> map) {
Set<String> set = map.get(key);
if (set == null) {
set = new HashSet<String>();
map.put(key, set);
}
set.add(value);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4833 次 |
| 最近记录: |