indexedseq

Scala Seq class: methods, examples, and syntax

This page contains a large collection of examples of how to use the methods on the Scala Seq class.

Important note about Seq, IndexedSeq, and LinearSeq

As an important note, I use Seq in the following examples to keep things simple, but in your code you should be more precise and use IndexedSeq or LinearSeq where appropriate. As the Seq class Scaladoc states:

Make the Scala Vector class your default immutable sequence

This is an excerpt from the Scala Cookbook. This is Recipe 10.7, “Make the Vector Class Your ‘Go To’ Immutable Sequence.”

Problem

You want a fast, general-purpose, immutable, sequential collection type for your Scala applications.

Solution

The Vector class was introduced in Scala 2.8 and is now considered to be the “go to,” general-purpose immutable sequential data structure.

How to choose a collection class in Scala

This is an excerpt from the Scala Cookbook (partially re-worded for the internet). This is Recipe 10.2.

Scala FAQ: How do I choose a Scala collection class to solve a particular problem?

Solution

To being with, there are three main categories of collection classes to choose from:

Understanding the Scala collections hierarchy

This is an excerpt from the Scala Cookbook. (This is Recipe 10.1.)

Overview: The Scala collections hierarchy is very rich (both deep and wide), and understanding how it’s organized can be helpful when choosing a collection to solve a problem.

Solution

Figure 10-1, which shows the traits from which the Vector class inherits, demonstrates some of the complexity of the Scala collections hierarchy.