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

1. Immutable List-Like Collections

1.1 List

  • Description: List is a linear, immutable collection that maintains order. It is the most commonly used collection in Scala for sequences of elements.
  • Characteristics:
    • Immutable: Once created, it cannot be modified.
    • Linked List: Internally implemented as a singly linked list.
    • Efficient prepends (::), but appends (:+) are O(n).
  • Use Case: General-purpose sequence where immutability is desired.
val list = List(1, 2, 3)

1.2 Vector

  • Description: Vector is an immutable indexed sequence that offers an alternative to List. It is designed to provide more efficient random access and updates.
  • Characteristics:
    • Immutable: Once created, it cannot be modified.
    • Indexed: Provides efficient random access (O(log32 n)).
    • Fast prepend, append, and random access.
  • Use Case: When you need fast random access and efficient append/prepend operations.
val vector = Vector(1, 2, 3)

1.3 Seq

  • Description: Seq is a general trait for ordered collections. List and Vector are common implementations of Seq.
  • Characteristics:
    • Immutable: Seq is immutable by default in the scala.collection.immutable package.
    • Maintains order.
  • Use Case: When you want to work with sequences generically without worrying about the specific implementation.
val seq: Seq[Int] = Seq(1, 2, 3)  // Immutable by default

1.4 Range

  • Description: Range is a special type of sequence representing an ordered sequence of integers. It is often used for looping or generating sequences of numbers.
  • Characteristics:
    • Immutable.
    • Efficient representation of ranges of integers.
  • Use Case: When you need to generate a sequence of numbers or iterate over a range of integers.
val range = 1 to 10  // Range from 1 to 10

1.5 Stream (deprecated, use LazyList instead)

  • Description: Stream was a lazily evaluated list, where elements are computed as needed. It has been replaced by LazyList in Scala 2.13.
  • Characteristics:
    • Immutable.
    • Lazy: Elements are computed only when accessed.
  • Use Case: When working with potentially infinite sequences or when you want to delay computation of elements.
val lazyList = LazyList(1, 2, 3, 4, 5)

1.6 LazyList

  • Description: LazyList is the replacement for Stream, offering lazy evaluation with more consistent performance.
  • Characteristics:
    • Immutable.
    • Lazy: Elements are computed as needed.
  • Use Case: For lazy evaluation of sequences, particularly useful for infinite sequences.
val lazyList = LazyList.from(1)  // Infinite lazy list starting from 1

2. Mutable List-Like Collections

2.1 ListBuffer

  • Description: ListBuffer is a mutable collection that is efficient for building lists incrementally. Once constructed, it can be converted to an immutable List.
  • Characteristics:
    • Mutable: Supports in-place modification.
    • Efficient append/prepend operations.
    • Converts easily to an immutable List.
  • Use Case: When you need to build a list incrementally before converting it to an immutable list.
import scala.collection.mutable.ListBuffer

val listBuffer = ListBuffer(1, 2, 3)
listBuffer += 4
val immutableList = listBuffer.toList

2.2 ArrayBuffer

  • Description: ArrayBuffer is a mutable, indexed sequence that provides fast random access and append/prepend operations.
  • Characteristics:
    • Mutable.
    • Indexed: Allows random access.
    • Efficient append/prepend operations.
  • Use Case: When you need a mutable, indexed sequence with efficient access and update operations.
import scala.collection.mutable.ArrayBuffer

val arrayBuffer = ArrayBuffer(1, 2, 3)
arrayBuffer += 4

2.3 Array

  • Description: Array is a mutable, fixed-size sequence of elements. It provides fast access and update operations.
  • Characteristics:
    • Mutable.
    • Indexed: Allows random access.
    • Fixed size: The length of an array cannot be changed after creation.
  • Use Case: When you need a fixed-size, mutable sequence with efficient access and update operations.
val array = Array(1, 2, 3)
array(0) = 0

2.4 MutableList (deprecated, use ListBuffer instead)

  • Description: MutableList is a mutable linked list. It has been largely replaced by ListBuffer in most use cases.
  • Characteristics:
    • Mutable.
    • Linked list structure.
  • Use Case: Use ListBuffer instead for most cases.

3. Other List-Like Collections

3.1 Queue

  • Description: Queue is a mutable collection that allows elements to be added at the end and removed from the front (FIFO - First In, First Out).
  • Characteristics:
    • Mutable.
    • FIFO: First In, First Out order.
  • Use Case: When you need a mutable, ordered collection with FIFO semantics.
import scala.collection.mutable.Queue

val queue = Queue(1, 2, 3)
queue.enqueue(4)
queue.dequeue()  // Removes 1

3.2 Stack (deprecated, use List or Vector instead)

  • Description: Stack is a mutable collection that allows elements to be added and removed in a LIFO (Last In, First Out) manner. It has been replaced by using List or Vector for stack-like behavior.
  • Characteristics:
    • Mutable.
    • LIFO: Last In, First Out order.
  • Use Case: Use List or Vector with operations like :: and tail instead of Stack.
import scala.collection.mutable.Stack

val stack = Stack(1, 2, 3)
stack.push(4)
stack.pop()  // Removes 4

3.3 Deque

  • Description: Deque (Double-ended Queue) allows elements to be added and removed from both ends. Available as a mutable collection.
  • Characteristics:
    • Mutable.
    • Allows operations on both ends (FIFO and LIFO).
  • Use Case: When you need a mutable sequence that supports adding/removing elements from both ends.
import scala.collection.mutable.ArrayDeque

val deque = ArrayDeque(1, 2, 3)
deque.append(4)
deque.prepend(0)

4. Summary

Scala provides a wide range of list-like collections, each suited for different purposes:

  • Immutable Collections:

    • List: The go-to immutable sequence.
    • Vector: Better for random access.
    • LazyList: For lazy evaluation.
    • Range: For generating sequences of numbers.
  • Mutable Collections:

    • ListBuffer: For building lists incrementally.
    • ArrayBuffer: For fast indexed access.
    • Array: For fixed-size, mutable sequences.
    • Queue: For FIFO operations.
    • Deque: For double-ended operations.