Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Collections

Transforming Collections

Learn the core Scala collection methods: map, filter, flatMap, foldLeft, reduce, zip, partition, groupBy, sortBy, take/drop and views, with worked examples and diagrams.

The biggest productivity win in Scala is the family of collection methods. Instead of writing a loop with a counter and a temporary list, you describe the result: "keep the even numbers, square them, add them up". The methods are the same on List, Vector, Set, Option, Array and many other types. This lesson teaches the ones you will use every day, in the order you are likely to need them. Scala examples are read-only; runnable Java streams and Kotlin show the same pipelines.

map: change every element

map applies a function to each element and returns a collection of the results, the same size as the input.

SCALA
val nums = List(1, 2, 3, 4)
nums.map(_ * 2)                  // List(2, 4, 6, 8)
nums.map(n => s"#$n")            // List(#1, #2, #3, #4)
List("a", "bb").map(_.length)    // List(1, 2)

filter and friends: choose elements

SCALA
nums.filter(_ % 2 == 0)        // List(2, 4)
nums.filterNot(_ % 2 == 0)     // List(1, 3)
nums.find(_ > 2)               // Some(3)
nums.exists(_ > 3)             // true
nums.forall(_ > 0)             // true
nums.count(_ > 1)              // 3
nums.takeWhile(_ < 3)          // List(1, 2)
nums.dropWhile(_ < 3)          // List(3, 4)
nums.partition(_ > 2)          // (List(3, 4), List(1, 2))

find returns an Option because nothing may match. partition splits in one pass into a pair.

flatMap: map then flatten

When the function returns a collection for each element, map gives a list of lists. flatMap joins them:

SCALA
val words = List("hi there", "scala fun")
words.map(_.split(" ").toList)       // List(List(hi, there), List(scala, fun))
words.flatMap(_.split(" ").toList)   // List(hi, there, scala, fun)
List(List(1, 2), List(3)).flatten    // List(1, 2, 3)

flatMap is also what lets for comprehensions with several generators work. It also works with Option, where None disappears:

SCALA
List("1", "x", "3").flatMap(_.toIntOption)   // List(1, 3)

The diagram shows the three most common shapes:

flowchart LR
  subgraph map
    A1["1,2,3"] -->|"x2"| B1["2,4,6"]
  end
  subgraph filter
    A2["1,2,3,4"] -->|"even"| B2["2,4"]
  end
  subgraph fold
    A3["1,2,3"] -->|"sum"| B3["6"]
  end

map keeps the size, filter shrinks the collection, fold collapses it to one value.

fold and reduce: collapse to one value

SCALA
nums.sum                         // 10
nums.product                     // 24
nums.reduce(_ + _)               // 10
nums.reduce(_ max _)             // 4
nums.foldLeft(0)(_ + _)          // 10
nums.foldLeft("")((acc, n) => acc + n)   // "1234"
nums.foldRight(List.empty[Int])(_ :: _)  // List(1, 2, 3, 4)

reduce uses the first element as the starting value and fails on an empty collection. foldLeft(start)(f) takes an explicit start, works on empty input and may return a different type. The function receives the accumulated value first and the next element second.

Fold example with a different result type, counting vowels:

SCALA
"functional programming".foldLeft(0)((n, c) => if "aeiou".contains(c) then n + 1 else n)
// 7

Here is the same idea with Java streams, runnable:

Java map / filter / reduce in Java streams (comparison)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

In Scala the same pipeline reads nums.filter(_ % 2 == 0).map(n => n * n).sum, with no stream wrapper or terminal call.

Sorting and ranking

SCALA
val people = List(("Ann", 31), ("Bob", 25), ("Cy", 31))
people.sortBy(_._2)                   // List((Bob,25), (Ann,31), (Cy,31))
people.sortBy(p => -p._2)             // oldest first
people.maxBy(_._2)                    // (Ann,31)
people.minBy(_._2)                    // (Bob,25)
people.sortBy(p => (p._2, p._1))      // sort by age, then name

sortBy is stable: equal keys keep their original order.

groupBy and counting

SCALA
val byAge = people.groupBy(_._2)
// Map(31 -> List((Ann,31), (Cy,31)), 25 -> List((Bob,25)))

people.groupMap(_._2)(_._1)
// Map(31 -> List(Ann, Cy), 25 -> List(Bob))

List("a", "b", "a").groupBy(identity).view.mapValues(_.size).toMap
// Map(a -> 2, b -> 1)

groupBy makes a Map from key to the list of elements with that key. groupMap(key)(value) also transforms each element.

zip and zipWithIndex

SCALA
val names = List("a", "b", "c")
val ages = List(1, 2, 3)
names.zip(ages)           // List((a,1), (b,2), (c,3))
names.zipWithIndex        // List((a,0), (b,1), (c,2))
names.zip(ages).unzip     // (List(a, b, c), List(1, 2, 3))
names.zip(ages).toMap     // Map(a -> 1, b -> 2, c -> 3)

zip stops at the shorter list.

Slicing and windows

SCALA
val xs = List(1, 2, 3, 4, 5)
xs.take(2)        // List(1, 2)
xs.drop(2)        // List(3, 4, 5)
xs.slice(1, 4)    // List(2, 3, 4)
xs.sliding(2).toList   // List(List(1, 2), List(2, 3), List(3, 4), List(4, 5))
xs.grouped(2).toList   // List(List(1, 2), List(3, 4), List(5))
xs.splitAt(2)     // (List(1, 2), List(3, 4, 5))
xs.scanLeft(0)(_ + _)  // List(0, 1, 3, 6, 10, 15): running totals

Chaining

Because each method returns a collection, you chain them. Example: top three longest distinct words in a sentence:

SCALA
val sentence = "the quick brown fox jumps over the lazy dog"
sentence.split(" ")
  .distinct
  .sortBy(w => -w.length)
  .take(3)
  .toList
// List(quick, brown, jumps)

(Sort stability keeps quick, brown, jumps in their original order because they have equal length 5.)

views: avoid temporary collections

Each step of a chain builds an intermediate collection. For large data, call .view to make the chain lazy and finish with .toList:

SCALA
(1 to 1_000_000).view
  .map(_ * 2)
  .filter(_ % 3 == 0)
  .take(3)
  .toList
// List(6, 12, 18)

Only as many elements as needed are computed. Use views when the chain is long or the source is large; for small collections plain methods are simpler.

Method cheat sheet

Goal Method
Change each element map
Keep some filter, find, takeWhile
Flatten nested flatMap, flatten
One result sum, reduce, foldLeft
Test exists, forall, contains
Order sorted, sortBy, sortWith, reverse
Group groupBy, groupMap, partition
Combine zip, ++, zipWithIndex

Common mistakes

  • Using reduce on a possibly empty collection. Use foldLeft or reduceOption.
  • Using map just for side effects. Use foreach.
  • Mutating a var inside map or foreach.
  • Forgetting that map on a Set can shrink the result because duplicates vanish.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does List(1, 2, 3).map(_ * 2) return?

    map applies the function to each element and returns a new list of the same length.
  2. 2. What is the difference between map and flatMap?

    flatMap is map followed by flatten, so nested collections are merged.
  3. 3. Which method is safe on an empty list and lets you choose the start value?

    foldLeft(start)(f) returns the start value for an empty collection; reduce, head and max throw.
  4. 4. What does List(1, 2, 3, 4).partition(_ > 2) return?

    partition returns a pair: the elements that satisfy the predicate and the rest.
  5. 5. Why use .view in a long chain?

    A view applies the steps lazily, element by element, until you convert it to a concrete collection.

Summary

  • map, filter and flatMap build the result you describe; they never change the original.
  • fold/reduce/sum collapse a collection into one value; prefer foldLeft when it may be empty.
  • groupBy, partition, zip, sortBy, sliding and scanLeft cover most reshaping needs.
  • Chain methods to express a pipeline; use .view for large data.
  • The same method names work across List, Vector, Set, Option and more.

Next: Tuples, Options and Ranges