Collections · 26 of 42
Map(key1 -> value1, key2 -> value2, ...)Map is Predef.Map which points to scala.collection.immutable.MapHashMapval map1 = Map("one" -> 1, "two" -> 2, "three" -> 3)
//Map of type Map[String, Int]
val map2 = Map(1 -> "one", "2" -> 2.0, 3.0 -> false)
//Map of type Map[Any, Any]
import collection.mutable
val mmap = mutable.HashMap("a" -> 1, "b" -> 2, "c" -> 3)
//the "mutable version" of Map
//Maps remove duplicate keys:
println(Map("a" -> 1, "a" -> 2))
//Get items using map(key)
val one = map1("one")
//NoSuchElementException will be thrown if key doesn't exist!
//e.g. this code: val fourExists = map1("four")
//throws NoSuchElementException: key not found: four
//the get method returns an Option, which will be explained later
val fourExistsOption = map1.get("four")
println(one)
println(fourExistsOption.isDefined)
//You can set / modify items using map(key) = value
mmap("d") = 4
println(mmap)
//Concatenation using the ++ operator
//(removes duplicate keys, order not guaranteed)
//note: map2 (whose key type is Any) goes first,
//because ++ keeps the key type of the map on its left
val concatenated = map2 ++ map1 ++ mmap
println(concatenated)
//Concatenation doesn't modify the maps themselves
println(map1)
//Removing elements (mutable Maps only)
mmap -= "c"
println(mmap)
//Adding elements (mutable Maps only)
mmap += "e" -> 5
mmap ++= Map("f" -> 6, "g" -> 7)
println(mmap)
//Find
val personMap = Map(("Alice",1), ("Bob",2), ("Carol",3))
def findByName(name:String) = personMap.getOrElse(name, 4)
val findBob = findByName("Bob")
val findEli = findByName("Eli")
println(findBob)
println(findEli)