Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with immutable HashMap in Scala

Tags:

hashmap

scala

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?

like image 949
Alan Coromano Avatar asked Jan 14 '23 02:01

Alan Coromano


1 Answers

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

like image 52
4lex1v Avatar answered Jan 27 '23 19:01

4lex1v