Introduction
Scala’s collection classes are rich, deep, and differ significantly from the Java collections, all of which makes learning them a bit of a speed bump for developers coming to Scala from Java.
When a Java developer first comes to Scala, she might think, “Okay,
I’ll use lists and arrays, right?” Well, not really. The Scala List
class is very different from the Java
List
classes—including the part where
it’s immutable—and although the Scala Array
is an improvement on the Java array in
most ways, it’s not even recommended as the “go to” sequential collection
class.
Because there are many collections classes to choose from, and each
of those classes offers many methods, a goal of this chapter (and the
next) is to help guide you through this plethora of options to find the
solutions you need. Recipes will help you decide which collections to use
in different situations, and also choose a method to solve a problem. To
help with this, the methods that are common to all collections are shown
in this chapter, and methods specific to collections like List
, Array
,
Map
, and Set
are shown in Chapter 11.
A Few Important Concepts
There are a few important concepts to know when working with the methods of the Scala collection classes:
What a predicate is
What an anonymous function is
Implied loops
A predicate is simply a method, function, or
anonymous function that takes one or more parameters and returns a
Boolean value. For instance, the following method returns true
or false
, so it’s a predicate: ...
Get Scala Cookbook 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.