Option[T]是给定类型T的零或一个元素的一种容器。
scala> val capittals = Map("France"->"Paris","Japan"->"Tokyo")
captitals: scala.collection.immutable.Map[String,String] = Map(France -> Paris, Japan -> Tokyo)
scala> println(capittals.get("France"))
Some(Paris)
scala> println(capittals.get("India"))
None
还测试了 Option[T]值相等是什么意思:
val capitals = Map("France"->"Paris","Japan"->"Tokyo")
//map.get(..)方法返回的是Option[T],这个T应该是value对应的类型???
println(capitals.get("France"))
println(capitals.get("India"))
Console.println(capitals.get("Japan"))
println("-----------")
val capitals2 = Map("France"->10,"Japan"->11)
println(capitals2.get("France"))
println(capitals2.get("India"))
Console.println(capitals2.get("Japan"))
if(capitals2.get("France") == Some(10))
println("France ok")
if(capitals2.get("India") == None)
println("India None")
打印结果:
Some(Paris)
None
Some(Tokyo)
-----------
Some(10)
None
Some(11)
France ok
India None