Scala Collections in action
The Scala language supports a very rich Collection API. It supports two sets of Collections—one to support immutability (immutable Collections) and another to support mutability (mutable Collections).
It requires almost an entire book to explain each and every Collection API in depth. As we have only one subsection in this chapter for Collections, we will discuss only the most important and frequently used Collections here.
Scala List
In the Scala Collection API, List is a LIFO-based immutable Collection which represents an ordered linked list (where LIFO stands for Last In First Out). It is available as scala.collection.immutable.List
.
It has two implementation case classes, scala.Nil
and scala.::
as shown here:
sealed abstract class List[+A] object List final case class :: extends List[B] case object Nil extends List[Nothing]
Let's explore it with some examples:
scala> val numberList = List(1,2,3,4) numberList: List[Int] = List(1, 2, 3, 4) scala> number...