scala高阶函数之option

option是scala中是否有值函数

 

package day3

object demo_option {
    def main(args: Array[String]): Unit = {
        val contrymap = Map[String, String](
            "china" -> "beijing",
            "jpan" -> "dongjing",
            "USA" -> "huasdun"
        )
       // contrymap.filter(kv => kv._2.eq("beijing")) 过滤出北京这一个值
        println(contrymap.filter(_._2.eq("beijing")))  // Map(china -> beijing)
        contrymap.filter(_._2.eq("beijing")).get("china") match {
            case Some(captal) => println(s"chian's captal is ${captal}") //chian's captal is beijing
            case None => println("没有这个国家")
        }
    }
}

 

posted on 2021-08-31 15:26  孔扎根  阅读(91)  评论(0编辑  收藏  举报

导航