Immutable and mutable collections
A collection is used to contain data that is used by the program later in time. In a multithreaded environment, if multiple threads try to access a collection at the same time, this can give you a hard time debugging what went wrong. That is a problem programmers usually face when working with collections in a multithreaded environment. But there's a universal solution for that, which expects you to use an immutable collection. Immutable means you can't change/mutate it. Scala provides you options to choose from: root
, mutable
, and immutable
collections. These three are variants that exist in three separate packages: scala.collection
,scala.collection.mutable
, and scala.collection.immutable
. If you don't specify the collection and use one, it'll be an immutable one by default. But how do these work, exactly? Let's take a look:
scala> val studentsPresent = List("Alex", "Bob", "Chris") studentsPresent: List[String] = List(Alex, Bob, Chris)
A collection...