Scalaでのパターンマッチング——Optionタイプでのパターンマッチング

2610 ワード

/**
  * Option        
  * Option       ,   Some None(    )
  * None   case object,  Some   extends Option ,   Some case class,
  *   case class        , case object       ?
  *               ,case object case class     :
  * case object       CaseObject$.class    apply、unapply  ,
  *     None              ,      ,     case object      。
  */
object OptionDemo {
  def main(args: Array[String]): Unit = {
    val map = Map("hadoop" -> 1, "spark" -> 2, "flink" -> 3)
    //scala                  
    def mapPattern(key: String): Unit = {
      //get(key)   ,  key         value 
      map.get(key) match {
        case Some(x)=>println(x);x
        case None=>println("None");-1
      }
    }
    println(mapPattern("hive"))
    println(mapPattern("spark"))
  }
}