O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​(​"blog.toolshed.com"​, ​"pragdave.me"​, ​"blog.agiledeveloper.com"​)

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required