如何在HashMap内的HashSet中添加值?
Map<String, Set<String>> myMap;
答案 0 :(得分:3)
get
Set null
!put
。 Set<String> theSet = myMap.get(aKey);
if (theSet == null) {
theSet = new HashSet<String>();
myMap.put(aKey, theSet);
}
theSet.add(value);
// ...
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);
}