I have an immutable HashMap and want to add/remove values from it. The Scala api docs say that I have to use +=
and -=
methods, but they don't work and I get the following error:
error: value += is not a member of scala.collection.immutable.HashMap
How do I add or remove values from a HashMap in Scala?
You are watching api for mutable
HashMap, to add pair to immutable HashMap use +
hashMap + ("key", "value")
or if you want to remove use -
hashMap - "key"
But you should remember that it would create a new structure
As for +=
method, i think that this design is not good, because in such case you have to use var
instead of val
, and that's not a functional way
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