Scala Language For Expressions Iterate Through Collections Using a For Loop


Example

This demonstrates how to print each element of a Map

val map = Map(1 -> "a", 2 -> "b")
for (number <- map) println(number) // prints (1,a), (2,b)
for ((key, value) <- map) println(value) // prints a, b

This demonstrates how to print each element of a list

val list = List(1,2,3)
for(number <- list) println(number) // prints 1, 2, 3