I have code like this:
val dm = List[String]()
val dk = List[Map[String,Object]]()
.....
dm.add("text")
dk.add(Map("1" -> "ok"))
but it throws runtime java.lang.UnsupportedOperationException.
I need to declare empty list or empty maps and some where later in the code need to fill them.
Scala lists are immutable by default. You cannot “add” an element, but you can form a new list by appending the new element in front. Since it is a new list, you need to reassign the reference (so you can’t use a val).
The operator
::creates the new list. You can also use the shorter syntax:NB: In scala don’t use the type
ObjectbutAny,AnyReforAnyVal.