Chapter 8. Using the right collection

 

In this chapter

  • Determining the appropriate collection for an algorithm
  • Descriptions of immutable collection types
  • Descriptions of mutable collection types
  • Changing the execution semantics of a collection from strict to lazy and back
  • Changing the execution semantics of a collection from sequential to parallel and back
  • Writing methods for all collection types

The Scala collections library is the single most impressive library in the Scala ecosystem. It’s used in every project and provides myriad utility functions. The Scala collections provide many ways of storing and manipulating data, which can be overwhelming. Because most of the methods defined on Scala collections are available on every collection, it’s important to know what the collection types imply in terms of performance and usage patterns.

Scala’s collections also split into three dichotomies:

  • Immutable and mutable collections
  • Eager and delayed evaluation
  • Sequential and parallel evaluation

Each of these six categories can be useful. Sometimes parallel execution can drastically improve throughput, and sometimes delaying the evaluation of a method can improve performance. The Scala collections library provides the means for developers to choose the attributes their collections should have. We’ll discuss these in sections 8.2 through 8.4

8.1. Use the right collection

8.2. Immutable collections

8.3. Mutable collections

8.4. Changing evaluation with views and parallel collections

8.5. Writing methods to use with all collection types

8.6. Summary