shouldn't this map.remove(key) be inside of the if block ? because we try to remove the key that is not existing in the map.