Scala Map foreach

后端 未结 7 1953
刺人心
刺人心 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)} 
    
    0 讨论(0)
  • 2021-01-31 14:39

    I'm not sure about the error, but you can achieve what you want as follows:

    m.foreach(p => println(">>> key=" + p._1 + ", value=" + p._2))
    

    That is, foreach takes a function that takes a pair and returns Unit, not a function that takes two arguments: here, p has type (String, Int).

    Another way to write it is:

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

    In this case, the { case ... } block is a partial function.

    0 讨论(0)
  • 2021-01-31 14:42

    The confusing error message is a compiler bug, which should be fixed in 2.9.2:

    0 讨论(0)
  • 2021-01-31 14:49

    oops, read the doco wrong, map.foreach expects a function literal with a tuple argument!

    so

    m.foreach((e: (String, Int)) => println(e._1 + "=" + e._2))
    

    works

    0 讨论(0)
  • 2021-01-31 14:49

    Docs says argument is tuple -> unit, so We can easily do this

    Map(1 -> 1, 2 -> 2).foreach(tuple => println(tuple._1 +" " + tuple._2)))
    
    0 讨论(0)
  • 2021-01-31 14:52

    Excellent question! Even when explicitly typing the foreach method, it still gives that very unclear compile error. There are ways around it, but I can't understand why this example does not work.

    scala> m.foreach[Unit] {(key: String, value: Int) => println(">>> key=" + key + ", value=" + value)}
    <console>:16: error: type mismatch;
     found   : (String, Int) => Unit
     required: (String, Int) => Unit
                  m.foreach[Unit] {(key: String, value: Int) => println(">>> key=" + key + ", value=" + value)}
                                                             ^
    
    0 讨论(0)
提交回复
热议问题