Title: Iterate Over A Map
Slug: iterate_over_a_map
Summary: Iterate Over A Map Using Scala.
Date: 2017-04-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon
If you want to learn more, check out Scala Cookbook and Programming in Scala.
In [1]:
// Create a map with three key value pairs
val prices = Map("Video Card" -> 200.00,
"Motherboard" -> 400.00,
"CPU" -> 100.00)
In [6]:
// for each key and value in prices
for ((k,v) <- prices) yield {
// Return the value plus 100
v+100
}
Out[6]:
In [9]:
// Increase each value in the map by 1000
prices.mapValues(_+1000)
Out[9]: