Common Scala Collections

Scala has three main types of collections:

  • List—as you’d expect, an ordered collection of objects

  • Set—an unordered collection

  • Map—a dictionary of key-value pairs

Scala favors immutable collections, even though mutable versions are also available. If you want to modify a collection and if all the operations on the collection are all within a single thread, you can choose a mutable collection. However, if you plan to use the collection across threads or actors, the immutable collections are better. Immutable collections are thread safe, free from side effects, and help with program correctness. You can choose between these versions by selecting a class in one of these two packages: scala.collection.mutable or scala.collection.immutable ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.