Option[T]是給定類型T的零或一個元素的一種容器。api
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
scala> println(capittals.get("India"))
Noneget
還測試了 Option[T]值相等是什麼意思:it
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")io
打印結果:table
Some(Paris)
None
Some(Tokyo)
-----------
Some(10)
None
Some(11)
France ok
India None容器