We’ve only discussed immutable collections so far. As mentioned, we get this capability by default. To create mutable collections, we have to do an explicit import or reference to the class name.
|||val capitals =|
|||scala.collection.mutable.Map("New York" -> "Albany", "Texas" -> "Austin")|
|||capitals("Oregon") = "Salem"|
|||println("After change: " + capitals)|
The output from the preceding code is
|||Map(Texas -> Austin, New York -> Albany)|
|||After change: Map(Oregon -> Salem, Texas -> Austin, New York -> Albany)|
Unlike its immutable counterpart, the mutable version of the map provides ways to insert and remove keys from the collection. Likewise, a ListBuffer allows mutation ...