What is difference between plus() and put() methods in kotlin map?
I have created the map and use plus() method to add my key value pair but when I print the map, I can't find the added values. And then I have moved to put(). What is the behaviour of plus() method?
As described in the documentation, the plus
method (which is normally used in its operator form, +
) returns a new map containing the elements from the original map and the key/value pair passed to it as a parameter. It does not modify the map on which it was called.
The idiomatic way to add values to a Kotlin map is not to use the put
method, but to use square brackets:
map[key] = value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With