Maps

Scala / Basics

// maps, hash tables, with key:value pairs are immutable
val map_any = Map("Adam" -> List("[email protected]", 2445055),
                  "Bard" -> "[email protected]" )

map_any("Adam")                                     // List([email protected], 2445055)
map_any.contains("Bard")                            // true
map_any.exists(_ == "Bard" -> "[email protected]")     // true

// mutable map
val map_mutable = scala.collection.mutable.Map[String, Int]()

map_mutable += ("Alpha" -> 1, "Beta" -> 2)          // Map(Beta -> 2, Alpha -> 1)
map_mutable -= "Beta"                               // Map(Alpha -> 1)
map_mutable("Alpha") = 100                          // Map(Alpha -> 100)

// iterating maps
val map: Map[String, Int] = Map("Alpha" -> 1, "Beta" -> 2)

for ((k, v) <- map) {
    println(k, v)                                   // (Alpha,1) (Beta,2)
}

'''
Michael Sjoeberg
2018-11-05
https://github.com/michaelsjoeberg/scala-playground/blob/master/basic-syntax/maps.scala
'''