Collections (Advanced)
In this lesson, you will dive deeper into Scala’s advanced collections. Scala provides one of the most powerful and expressive collection libraries of any programming language, combining immutability, performance, and functional programming.
Advanced collection usage is essential for writing clean, efficient, and scalable Scala code, especially in real-world applications and data processing.
Immutable vs Mutable Collections
Scala collections are divided into two main categories:
- Immutable collections (default and recommended)
- Mutable collections (used only when necessary)
Immutable collections never change their contents. Any operation returns a new collection.
val nums = List(1, 2, 3)
val newNums = nums :+ 4
println(nums)
println(newNums)
This approach makes programs safer and easier to reason about.
Advanced List Operations
Lists support powerful transformations using higher-order functions.
val data = List(1, 2, 3, 4, 5)
val squared = data.map(x => x * x)
val even = data.filter(_ % 2 == 0)
val sum = data.reduce(_ + _)
println(squared)
println(even)
println(sum)
These operations replace traditional loops with concise, expressive code.
Advanced Map Operations
Maps store key–value pairs and are widely used in real applications.
val prices = Map("apple" -> 100, "banana" -> 40, "orange" -> 60)
val discounted = prices.map {
case (item, price) => (item, price * 0.9)
}
println(discounted)
Pattern matching makes Map transformations very readable.
FlatMap Explained
flatMap is used when each element produces a collection and you want a single result.
val words = List("Scala", "is", "powerful")
val chars = words.flatMap(word => word.toList)
println(chars)
This avoids nested collections and simplifies data pipelines.
Grouping Data with groupBy
The groupBy method is extremely useful for analytics and reporting.
val numbers = List(1, 2, 3, 4, 5, 6)
val grouped = numbers.groupBy(_ % 2 == 0)
println(grouped)
This groups values based on a condition.
Partitioning Collections
partition splits a collection into two parts based on a condition.
val (evens, odds) = numbers.partition(_ % 2 == 0)
println(evens)
println(odds)
Collect with Partial Functions
The collect method combines filtering and mapping.
val mixed = List(1, "Scala", 2, "FP")
val onlyInts = mixed.collect {
case i: Int => i * 10
}
println(onlyInts)
This is cleaner than using separate filter and map calls.
Views for Performance Optimization
Scala collections support views to delay computation until needed.
val large = (1 to 1000000).toList
val result = large.view
.map(_ * 2)
.filter(_ % 3 == 0)
.take(5)
.toList
println(result)
Views improve performance by avoiding unnecessary intermediate collections.
Why Advanced Collections Matter
Advanced collection operations allow you to:
- Write concise and expressive code
- Avoid bugs caused by mutable state
- Build scalable data pipelines
- Prepare for big data and Spark usage
📝 Practice Exercises
Exercise 1
Create a list of numbers and filter out numbers greater than 10.
Exercise 2
Group a list of words by their length.
Exercise 3
Use collect to extract only strings from a mixed list.
✅ Practice Answers
Answer 1
val nums = List(5, 12, 8, 20)
nums.filter(_ > 10)
Answer 2
val words = List("Scala", "Java", "FP", "Code")
words.groupBy(_.length)
Answer 3
val mixed = List(1, "Scala", true, "Code")
mixed.collect { case s: String => s }
What’s Next?
In the next lesson, you will learn about Monads and understand how Scala uses them to manage computations, errors, and data flow in a functional way.