Chapter 6. Common Collections
A collections framework provides data structures for collecting one or more values of a given type such as arrays, lists, maps, sets, and trees. Most of the popular programming languages have their own collections framework (or, at the least, lists and maps) because these data structures are the building blocks of modern software projects.
The term âcollectionsâ was popularized by the Java collections library, a high-performance, object-oriented, and type-parameterized framework. Because Scala is a JVM language, you can access and use the entire Java collections library from your Scala code. Of course, if you did, you would miss out on all the glory of the higher-order operations in Scalaâs own collections.
Scala has a high-performance, object-oriented, and type-parameterized collections framework just as Java does. However, Scalaâs collections also have higher-order operations like map
, filter
, and reduce
that make it possible to manage and manipulate data with short and expressive expressions. It also has separate mutable versus immutable collection type hierarchies, which make switching between immutable data (for stability) and mutable data (when necessary) convenient.
The root of all iterable collections, Iterable
, provides a common set of methods for (you guessed it) iterating through and manipulating collection data. Weâll now explore some of its most popular and immutable collections.
Lists, Sets, and Maps
Letâs start with the List ...
Get Learning 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.