Tuple & Map
Tuple
Tuple is immutable. It's just like that in Python.
val tuple = (1, "Hello")
To access member, Tuple has _1 and _2
println(tuple._1) // 1
println(tuple._2) // Hello
We can copy a tuple, and replace the elements in copy.
println(tuple.copy(_2 = "Hi")) // (1, "Hi")
Map
Map is the data structure that has key - value
val bank: Map[String, Int] = Map(("Jack", 100), "Jay" -> 200)
"Jay" -> 200is equivalent to ("Jay", 200)
Basic operations
-
check if the
keyexistsprintln(bank.contains("Jack")) // true -
visit the
valuebykeyprintln(bank("Jack")) // 100 -
add a pair
Since
Mapis immutable, we need to create a new map.val newClient = "Mary" -> 300 val newBank = bank + newClient
Functionals
-
mapval newBank = bank.map(pair => pair._1.toLowerCase() -> pair._2) -
filterKeysval jBank = bank.view.filterKeys(key => key.startsWith("J")).toMap; -
mapValuesval richBank = bank.view.mapValues(value => value + 100).toMap;
Conversion to other collections
-
toList:bank.toList // List((Jack,100), (Alice,100)) -
toMap:val list = List(("Dan", 100)) println(list.toMap) // Map(Dan -> 100) -
groupBy: It transforms a list to map.val names = List("Jack", "Jay", "May", "Mark") val nameGroup = names.groupBy(name => name.charAt(0)) // HashMap(J -> List(Jack, Jay), M -> List(May, Mark))