I have a doubt and it is that when working with maps I don't know the difference between both methods, I know that put is to assign a key/value pair, but replace? As it says the name will be to replace either the key or value, but I am not clear with both methods. I hope you can help me
Although
put
theyreplace
are similar, they are not exactly the same:put
: associates the value with the key in the map.replace
: associates the value with the key in the map, only if the key already exists in the map.The difference is whether the key has an associated value or not:
put
will bind the value to that key, whilereplace
doing nothing.