List
1. Immutable List-Like Collections
1.1 List
- Description:
Listis 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:
Vectoris an immutable indexed sequence that offers an alternative toList. 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:
Seqis a general trait for ordered collections.ListandVectorare common implementations ofSeq. - Characteristics:
- Immutable:
Seqis immutable by default in thescala.collection.immutablepackage. - Maintains order.
- Immutable:
- 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:
Rangeis 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:
Streamwas a lazily evaluated list, where elements are computed as needed. It has been replaced byLazyListin 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:
LazyListis the replacement forStream, 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:
ListBufferis a mutable collection that is efficient for building lists incrementally. Once constructed, it can be converted to an immutableList. - 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:
ArrayBufferis 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:
Arrayis 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:
MutableListis a mutable linked list. It has been largely replaced byListBufferin most use cases. - Characteristics:
- Mutable.
- Linked list structure.
- Use Case: Use
ListBufferinstead for most cases.
3. Other List-Like Collections
3.1 Queue
- Description:
Queueis 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:
Stackis a mutable collection that allows elements to be added and removed in a LIFO (Last In, First Out) manner. It has been replaced by usingListorVectorfor stack-like behavior. - Characteristics:
- Mutable.
- LIFO: Last In, First Out order.
- Use Case: Use
ListorVectorwith operations like::andtailinstead ofStack.
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.