Screen 1 of 5

Scala's Collection Universe

Think of collections as different types of storage containers. A freezer (immutable β€” locked), a fridge (mutable β€” swap items in/out), each with its own purpose.

CollectionTypeOrdered?Duplicates?Use Case
ListImmutableβœ…βœ…Default go-to sequence
VectorImmutableβœ…βœ…Fast random access
SetImmutable❌❌Unique elements only
MapImmutable❌Keys uniqueKey-value lookups
ArrayBufferMutableβœ…βœ…When you need to grow/shrink
HashMapMutable❌Keys uniqueFrequently changing map
πŸ’‘ Default is Immutable
In Scala, when you write List(...) or Map(...), you get the immutable version by default. This is a deliberate design choice for thread safety and functional purity. You have to explicitly import mutable collections.
Screen 2 of 5

The Big Three: map, flatMap, foreach

These are the most tested operations in Scala interviews. Think of them as three different conveyor belt machines in a factory.

[1, 2, 3]
β†’
.map(_ * 2)
β†’
[2, 4, 6]
["hi world", "bye all"]
β†’
.flatMap(_.split(" "))
β†’
["hi","world","bye","all"]
[1, 2, 3]
β†’
.foreach(println)
β†’
prints each (returns nothing)

πŸ—ΊοΈ map

Transform each element 1-to-1. Input: N items β†’ Output: N items. Like stamping every letter in a pile.

πŸ—‚οΈ flatMap

Transform + flatten. Each element can produce multiple results, then they're merged into one flat list.

πŸ“’ foreach

Side-effects only. Print, log, save β€” but returns Unit (nothing). Don't use for transformations!

Screen 3 of 5

More Power Tools: filter, fold, reduce

Think of these as a workshop toolkit. Each tool solves a specific type of data problem.

val nums = List(1, 2, 3, 4, 5) // filter: keep only what matches nums.filter(_ > 3) // List(4, 5) // reduce: collapse into one value nums.reduce(_ + _) // 15 (sum all) // foldLeft: like reduce but with a start nums.foldLeft(0)(_ + _) // 15 nums.foldLeft(100)(_ + _) // 115 // Chaining: the real power nums .filter(_ % 2 == 0) // List(2, 4) .map(_ * 10) // List(20, 40) .reduce(_ + _) // 60
β†’ Start with [1,2,3,4,5].
β†’ filter: Keep only items greater than 3. Like a sieve.
β†’ reduce: Combine all items into one. Here: 1+2+3+4+5 = 15.
β†’ foldLeft: Same as reduce but you pick a starting value. Start at 0 β†’ same result. Start at 100 β†’ adds 15 to 100.
β†’ Chaining: Pipe data through multiple operations! Filter evens β†’ multiply by 10 β†’ sum them up.
🎯 Interview Trap
"What's the difference between fold and reduce?" β†’ reduce crashes on empty collections (no starting value). foldLeft takes an initial value, so it's safe on empty collections. Always prefer foldLeft in production.
Screen 4 of 5

Immutable vs Mutable β€” When to Use Which

Think of immutable as stone tablets (carved once, read forever) and mutable as a whiteboard (erase and rewrite anytime).

AspectImmutable (default)Mutable
Thread safetyβœ… Safe to share⚠️ Need synchronization
PerformanceSlight overhead on updatesFaster updates in-place
DebuggingEasier (data never changes)Harder (who changed it?)
FP styleβœ… Preferred❌ Avoid if possible
Import needed?No (default)Yes: import scala.collection.mutable
// Immutable: creates NEW collection each time val list1 = List(1, 2, 3) val list2 = list1 :+ 4 // List(1,2,3,4) // list1 is still List(1,2,3)! // Mutable: modifies in place import scala.collection.mutable val buf = mutable.ArrayBuffer(1, 2, 3) buf += 4 // buf is now ArrayBuffer(1,2,3,4)
β†’ Adding to an immutable list creates a brand new list. The original is untouched.
β†’ This is great for safety but creates more objects.
β†’ Mutable buffer changes itself. Fast but dangerous in multi-threaded code.
Screen 5 of 5

Test Yourself 🧠

Q1: What does List("a b", "c d").flatMap(_.split(" ")) return?
List(List("a","b"), List("c","d"))
List("a", "b", "c", "d")
List("a b", "c d")
A compile error
Q2: You call List().reduce(_ + _) on an empty list. What happens?
Returns 0
Returns None
Throws an UnsupportedOperationException (runtime crash)
Returns an empty list
Q3: When building a concurrent web server, which collection type should you prefer?
mutable.ArrayBuffer β€” fastest updates
Immutable collections β€” thread-safe without locks
Java ArrayList β€” more compatible
Array β€” lowest level is best