Scala Map foreach

后端 未结 7 1951
刺人心
刺人心 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:57

    Yet another way:

    Map(1 -> 1, 2 -> 2).foreach(((x: Int, y: Int) => ???).tupled)
    

    However it requires explicit type annotations, so I prefer partial functions.

提交回复
热议问题