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" -> 200 is equivalent to ("Jay", 200)

Basic operations

  • check if the key exists

    println(bank.contains("Jack")) // true
    
  • visit the value by key

    println(bank("Jack")) // 100
    
  • add a pair

    Since Map is immutable, we need to create a new map.

    val newClient = "Mary" -> 300
    val newBank = bank + newClient
    

Functionals

  • map

    val newBank = bank.map(pair => pair._1.toLowerCase() -> pair._2)
    
  • filterKeys

    val jBank = bank.view.filterKeys(key => key.startsWith("J")).toMap;
    
  • mapValues

    val 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))