Why it matters

Wrong collection choice is the top Scala performance mistake. Using List where Vector is better, using immutable Map where mutable is faster in a hot loop — these small choices add up. Understanding the trade-offs is worth learning early.

Advertisement

The architecture

List: singly-linked list, immutable, prepend is O(1). Great for stack-like patterns; bad for random access.

Vector: tree-backed, immutable, effectively random access O(log32 N). The go-to immutable sequence.

Array: mutable, primitive-backed, direct access. Use for perf-critical code with known size.

Sequence typesListprepend O(1)Vectorrandom O(log32)Arraymutable primitiveChoose by access pattern: List for stack, Vector for general, Array for hot inner loop
Common sequence types.
Advertisement

How it works end to end

Map: immutable HashMap by default, ordered TreeMap available. Use immutable everywhere except in hot inner loops.

Set: immutable HashSet by default. Similar rules as Map.

Views: '.view' delays computation of transformations. Useful for large collections where you only need part of the result.

Parallel collections: '.par' distributes operations across threads. Careful — often not worth the overhead for small collections.