An Introduction to the Collection Library

In the following lesson, you will be introduced to Scala's collection library.

Introduction

You can think of collections as vessels used for collecting data and Scala has a vast library of them. Scala’s collection library is made up of classes and traits which provide you a plethora of built-in data structures and methods for collecting and manipulating data.

Traits is a topic for another course. For the scope of this course, you can safely assume that a trait is simply a class with its own members. An instance of a trait is identical to saying an instance of a class. Hence, we will be omitting the use of trait from this point onwards and just use the term class for both.

The focus of this chapter will be to simply introduce Scala’s collection library and focus on the syntax and implementation of the well-known/most used collections.

Each Scala collection is one of two types: mutable collection or immutable collection.

Mutable Collections

Mutable collections are collections which can be updated. Elements can be added to the collection and can be removed or manipulated. In this case, the collection itself will be getting modified.

Immutable Collections

Immutable collections cannot be updated. When you add, remove, or manipulate an element in an immutable collection, you are creating a new collection and leaving the old one unchanged.

Sequences, Sets, and Maps

The collection library takes on a hierarchical structure. At the top of the library, there are three main categories of collection classes under which different collections lie:

  • Sequences - Seq
  • Sets - Set
  • Maps - Map

All three classes contain both mutable and immutable collections.

Remember, Seq here would be acting as a blueprint with which you can create objects that represent sequences.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy