containsKey check for hashmap <p>I have read multiple posts to understand this, but I can't seem to quite get in on why we do a check if a map does not contain a key before performing a put operation? For eg,</p> <pre><code> if(!myMap.containsKey(myKey) { myMap.put(myKey,myValue); } </code></pre> <p>Why do we need this check? Either way, map doesn't allow duplicate key, and replaces the key with the new value if the key already exists. So why do we need to check this explicitly? Is this check needed for all implementations of Map? I am sorry if this is a very basic question. I can't seem to find an answer to this exact question. If there are posts that answer this that I may have missed, please point me to them, and feel free to mark mine as duplicate.</p>