List, Vector, Set, Map
Core collections.
List, Vector, Set, Map is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Scala's core collections
Scala offers several immutable collections in its standard library. The four you will use most are List, Vector, Set, and Map. Each has different performance and semantics.
object Main {
def main(args: Array[String]): Unit = {
val list = List(1, 2, 3)
val vector = Vector(1, 2, 3)
val set = Set(1, 2, 3)
val map = Map("a" -> 1, "b" -> 2)
println(list)
println(vector)
println(set)
println(map)
}
}List: a linked list
List is a singly-linked list. Prepending with :: is O(1) and fast, but random access and appending are O(n). It is ideal for recursion and stack-like use.
object Main {
def main(args: Array[String]): Unit = {
val xs = List(2, 3, 4)
val prepended = 1 :: xs
println(prepended)
println("head: " + xs.head)
println("tail: " + xs.tail)
}
}Vector: balanced and general
Vector is an indexed sequence with effectively O(1) access, update, prepend, and append. When you need fast random access or a general-purpose sequence, prefer Vector over List.
object Main {
def main(args: Array[String]): Unit = {
val v = Vector(10, 20, 30, 40)
println(v(2))
val updated = v.updated(0, 99)
println(updated)
println(v :+ 50)
}
}Set: unique elements
A Set stores distinct elements with no duplicates and no guaranteed order. Membership tests with contains are fast.
object Main {
def main(args: Array[String]): Unit = {
val s = Set(1, 2, 2, 3, 3, 3)
println(s)
println(s.contains(2))
println(s + 4)
println(s - 1)
}
}Set operations
Sets support mathematical operations: union (|), intersect (&), and diff (−−).
object Main {
def main(args: Array[String]): Unit = {
val a = Set(1, 2, 3)
val b = Set(2, 3, 4)
println(a union b)
println(a intersect b)
println(a diff b)
}
}Map: key-value pairs
A Map associates keys with values. Keys are unique. Create entries with the -> arrow, and look up values with get (returns Option) or apply.
object Main {
def main(args: Array[String]): Unit = {
val ages = Map("Ann" -> 30, "Bob" -> 25)
println(ages("Ann"))
println(ages.get("Cara"))
println(ages.getOrElse("Cara", 0))
}
}Updating a Map immutably
Immutable maps return a new map when you add or remove entries; the original is unchanged. Use + to add or overwrite and - to remove.
object Main {
def main(args: Array[String]): Unit = {
val m = Map("a" -> 1)
val m2 = m + ("b" -> 2)
val m3 = m2 - "a"
println(m)
println(m2)
println(m3)
}
}Iterating over a Map
Iterating a Map gives you key-value tuples. You can destructure them directly in a for-comprehension or with pattern matching.
object Main {
def main(args: Array[String]): Unit = {
val scores = Map("math" -> 90, "art" -> 75)
for ((subject, score) <- scores) {
println(s"$subject: $score")
}
}
}Common methods shared by all
All these collections share a large common API: size, isEmpty, map, filter, foreach, and more. Learn the API once and it applies everywhere.
object Main {
def main(args: Array[String]): Unit = {
println(List(1, 2, 3).map(_ * 2))
println(Vector(1, 2, 3).map(_ * 2))
println(Set(1, 2, 3).map(_ * 2))
}
}Converting between collections
Conversion methods like toList, toVector, toSet, and toMap let you switch types easily. Converting to a Set removes duplicates.
object Main {
def main(args: Array[String]): Unit = {
val withDupes = List(1, 1, 2, 3, 3)
println(withDupes.toSet)
val pairs = List(("a", 1), ("b", 2))
println(pairs.toMap)
}
}Choosing the right collection
Quick guide:
List— recursion, fast prepend, head/tail processingVector— general-purpose, fast indexed accessSet— uniqueness and membership testsMap— key-based lookups
object Main {
def main(args: Array[String]): Unit = {
val ids = List(5, 3, 5, 1, 3)
val unique = ids.toSet
val indexed = ids.toVector
println(s"unique count: ${unique.size}")
println(s"third element: ${indexed(2)}")
}
}Quick Check
Which collection automatically removes duplicate elements?
Recap
You met Scala's core immutable collections:
- List — linked list, fast prepend
- Vector — indexed, well-balanced performance
- Set — unique elements with set algebra
- Map — key-value lookups with
get/getOrElse
They share a rich common API and convert into one another easily.
Frequently asked questions
Is the “List, Vector, Set, Map” lesson free?
Yes — the full text of “List, Vector, Set, Map” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “List, Vector, Set, Map”?
Core collections. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “List, Vector, Set, Map” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- List, Vector, Set, Map
- Transformations
- Folding and Reducing
- Grouping and Sorting