Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

List

A List in Scala is an immutable, ordered collection of elements. Once created, a List cannot be changed. It's a linked list, meaning that it consists of nodes, where each node contains a value and a reference to the next node.

Creating Lists

You can create a List using the List companion object.

val numbers = List(1, 2, 3, 4, 5)
val fruits = List("apple", "banana", "cherry")

Basic Operations on Lists

Accessing Elements

  • Head: The first element of the list.
  • Tail: A list consisting of all elements except the head.
  • IsEmpty: Checks if the list is empty.
val numbers = List(1, 2, 3, 4, 5)

val head = numbers.head       // 1
val tail = numbers.tail       // List(2, 3, 4, 5)
val isEmpty = numbers.isEmpty // false

List Concatenation

You can concatenate lists using the ::: operator or the ++ method.

val list1 = List(1, 2, 3)
val list2 = List(4, 5, 6)

val concatenated = list1 ::: list2 // List(1, 2, 3, 4, 5, 6)
val concatenated2 = list1 ++ list2 // List(1, 2, 3, 4, 5, 6)

Prepending and Appending Elements

  • Prepend: Add an element to the beginning of the list using the :: operator.
  • Append: Add an element to the end of the list using the :+ method.
val numbers = List(2, 3, 4)

val withPrepended = 1 :: numbers     // List(1, 2, 3, 4)
val withAppended = numbers :+ 5      // List(2, 3, 4, 5)

Common List Methods

Mapping

The map method transforms each element of the list using a given function.

val numbers = List(1, 2, 3, 4, 5)
val squaredNumbers = numbers.map(n => n * n) // List(1, 4, 9, 16, 25)

Filtering

The filter method selects elements of the list that satisfy a predicate.

val numbers = List(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter(_ % 2 == 0) // List(2, 4)

Reducing

The reduce method combines the elements of the list using a binary operation.

val numbers = List(1, 2, 3, 4, 5)
val sum = numbers.reduce(_ + _)      // 15
val product = numbers.reduce(_ * _)  // 120

Sorting

The sorted method sorts a list based on the natural ordering of its elements or a provided comparator.

val numbers = List(5, 3, 1, 4, 2)
val sortedNumbers = numbers.sorted
println(sortedNumbers) // List(1, 2, 3, 4, 5)

val fruits = List("banana", "apple", "cherry")
val sortedFruits = fruits.sorted
println(sortedFruits) // List(apple, banana, cherry)

foreach

The foreach method applies a function to each element of the list.

val numbers = List(1, 2, 3, 4, 5)
numbers.foreach(println)

zip

The zip method combines two lists into a list of pairs.

val numbers = List(1, 2, 3)
val letters = List("a", "b", "c")
val zipped = numbers.zip(letters)
println(zipped) // List((1, "a"), (2, "b"), (3, "c"))

Immutable Nature of Lists

A key feature of List is its immutability. Once a List is created, it cannot be changed. Any operation that transforms a list will return a new list, leaving the original list unchanged.

val originalList = List(1, 2, 3)
val newList = originalList.map(_ * 2)

println(originalList) // List(1, 2, 3)
println(newList)     // List(2, 4, 6)