Title: For Loop A Map
Slug: for_loop_a_map
Summary: For Loop A Map Using Scala.
Date: 2017-01-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon

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

Create A Map


In [31]:
val vehicles = Map("vehicle_type" -> "Tank", 
                   "number" -> 21)

Loop With Value And Index


In [32]:
// Create a value for the returned values, for each key and value in the map,
val numberOfVehicles = for ((key, value) <- vehicles) yield {
                         // Return the values
                         value
                       }

In [33]:
// View the returned values
numberOfVehicles


Out[33]:
List(Tank, 21)