option在scala里面的作用和其他地方其实没区别,都是对键值对的一种获取返回的封装,因为直接展示null对程序不是很友好
package com.wy
object OptionDemo {
def main(args: Array[String]): Unit = {
val myMap: Map[String, String] = Map("key1" -> "value")
val value1: Option[String] = myMap.get("key1")
val value2: Option[String] = myMap.get("key2")
val k1: String = myMap.getOrElse("key1","value0")
val k3: String = myMap.getOrElse("key3","value0")
println(value1) // Some("value1")
println(value2) // None
println(k1)
println(k3)
}
}