Scala Map foreach

后端 未结 7 1965
刺人心
刺人心 2021-01-31 14:15

given:

val m = Map[String, Int](\"a\" -> 1, \"b\" -> 2, \"c\" -> 3)
m.foreach((key: String, value: Int) => println(\">>> key=\" + key + \",          


        
7条回答
  •  独厮守ぢ
    2021-01-31 14:35

    You need to patter-match on the Tuple2 argument to assign variables to its subparts key, value. You can do with very few changes:

    m.foreach{ case (key: String, value: Int) => println(">>> key=" + key + ", value=" + value)} 
    

提交回复
热议问题