I am having HashMap like this,
HashMap<String,Set<String>> map = new HashMap<String,Set<String>>();
I am trying to do before adding an element in map,
- Want to check whether the
keyexist or not, i can get it by usingmap.containsKey(). - If the key exist, i want check the size of
Setrespective to that key. - If
size <= 1i want add an element in that set.
if (map.containsKey("x")) {Set<String> s = map.get("x"); if (s.size() <= 1) { s.add("y");}}(oh, just misread that the key should be updated - why?!)