27 List

 

After reading this lesson, you will be able to:

  • Define an ordered sequence of items
  • Adding elements to an existing list
  • Traverse and transform its items using pattern matching

After mastering the Option type in the previous unit, in this lesson you are going to learn about List: many of the concepts you have mastered for optional values are also applicable to lists but in a slightly different context. The type List allows you to represent an immutable order sequence of elements. This concept is not exclusive to the Scala language. For example, in Java you use an ArrayList or a LinkedList, in C++ list is part of the Standard Template List (STL), in Python you can take advantage of list literals, in Javascript you can use arrays. You will find that lists in Scala are relatively similar to many other languages, with a fundamental difference: they are immutable by default. Using mutable lists is still possible, but discouraged. In this lesson, you will learn how to create a list and add elements to it. You will also see how to pattern match on lists. In the capstone, you will use lists to represent the information presented in the Movies Dataset.

27.1   Creating a List

Imagine you want to write a program to keep track of your contacts. You could present your data using a list data structure as follows:

27.2   Adding elements to a List

 

27.3   Pattern matching on a List

 
 

27.4   Summary

 

27.5   Answers to Quick Checks

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest