I did a search and was amazed that they had not asked before (at least I could not find him).
I have a map like this:
Map<String, String> myMap
I know that I can check if a key exists on the map using containsKey(Object key); , and I can replace the value with replace(String key, String value); and, of course, put the value using put(String key, String value);
Now, if I want to check the value, if it exists, update it, otherwise insert it, I have to use the condition:
if(myMap.containsKey(key)) { myMap.replace(key, value); } else { myMap.put(key, value); }
Is there a better way to do this? I personally feel that this is a little redundant and too complicated, which could be one line, not five!
java
Crazy dino
source share