Immutable Lists

Scala makes it easier and faster to access the first element of a list using the head method. Everything except the first element can be accessed using the tail method. Accessing the last element of the list requires traversing the list and so is more expensive than accessing the head and the tail. So, most operations on the list are structured around operations on the head and tail.

Let’s continue with the feeds example to learn about List. We can maintain an ordered collection of the feeds using a List:

val​ feeds = ​List​(​""​, ​""​, ​""​)

This creates an instance of List[String]. We can access the elements of the List using an index from 0 to list.length - 1. When we ...

Get Pragmatic Scala 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.