v Iterate Over A Map - Scala

Iterate Over A Map

If you want to learn more, check out Scala Cookbook and Programming in Scala.

Create A Map

// Create a map with three key value pairs
val prices = Map("Video Card" -> 200.00,
                 "Motherboard" -> 400.00,
                 "CPU" -> 100.00)

Loop Over A Map

// for each key and value in prices
for ((k,v) <- prices) yield {
    // Return the value plus 100
    v+100
}
List(300.0, 500.0, 200.0)

Apply Function To Each Map Value

// Increase each value in the map by 1000
prices.mapValues(_+1000)
Map(Video Card -> 1200.0, Motherboard -> 1400.0, CPU -> 1100.0)