模式匹配是Scala中非常有特色,非常強大的一種功能。模式匹配,其實類似於Java中的swich case語法,即對一個值進行條件判斷,然后針對不同的條件,進行不同的處理。
但是Scala的模式匹配的功能比Java的swich case語法的功能要強大地多,Java的swich case語法只能對值進行匹配。但是Scala的模式匹配除了可以對值進行匹配之外,還可以對類型進行匹配、對Array和List的元素情況進行匹配、對case class進行匹配、甚至對有值或沒值(Option)進行匹配。
而且對於Spark來說,Scala的模式匹配功能也是極其重要的,在spark源碼中大量地使用了模式匹配功能。因此為了更好地編寫Scala程序,並且更加通暢地看懂Spark的源碼,學好模式匹配都是非常重要的。
1、模式匹配的基礎語法(案例:成績評價)
// Scala是沒有Java中的switch case語法的,相對應的,Scala提供了更加強大的match case語法,即模式匹配,類替代switch case,match case也被稱為模式匹配 // Scala的match case與Java的switch case最大的不同點在於,Java的switch case僅能匹配變量的值,比1、2、3等;而Scala的match case可以匹配各種情況,比如變量的類型、集合的元素、有值或無值 // match case的語法如下:變量 match { case 值 => 代碼 }。如果值為下划線,則代表了不滿足以上所有情況下的默認情況如何處理。此外,match case中,只要一個case分支滿足並處理了,就不會繼續判斷下一個case分支了。(與Java不同,java的switch case需要用break阻止) // match case語法最基本的應用,就是對變量的值進行模式匹配 object test{ def main(args: Array[String]): Unit = { def studentScore(score:String): Unit ={ score match { case "A"=>println("excellent") case "B"=>println("good") case "C"=>println("soso") case _ =>println("you need work harder") } } studentScore("D") } } 在模式匹配中使用if守衛 // Scala的模式匹配語法,有一個特點在於,可以在case后的條件判斷中,不僅僅只是提供一個值,而是可以在值后面再加一個if守衛,進行雙重過濾 // 案例:成績評價(升級版) object test{ def main(args: Array[String]): Unit = { def studentScore(name:String,score:String): Unit ={ score match { case "A"=>println("excellent") case "B"=>println("good") case "C"=>println("soso") case _ if name=="leo"=>print(name+",you are good boy,come on!")//if守衛 case _ =>println("you need work harder") } } studentScore("leo","D") } } 在模式匹配中進行變量賦值 // Scala的模式匹配語法,有一個特點在於,可以將模式匹配的默認情況,下划線,替換為一個變量名,此時模式匹配語法就會將要匹配的值賦值給這個變量,從而可以在后面的處理語句中使用要匹配的值 // 為什么有這種語法??思考一下。因為只要使用用case匹配到的值,是不是我們就知道這個只啦!!在這個case的處理語句中,是不是就直接可以使用寫程序時就已知的值! // 但是對於下划線_這種情況,所有不滿足前面的case的值,都會進入_這種默認情況進行處理,此時如果我們在處理語句中需要拿到具體的值進行處理呢?那就需要使用這種在模式匹配中進行變量賦值的語法!! object test{ def main(args: Array[String]): Unit = { def studentScore(name:String,score:String): Unit ={ score match { case "A"=>println("excellent") case "B"=>println("good") case "C"=>println("soso") case _ if name=="leo"=>print(name+",you are good boy,come on!") case _score =>println("you need work harder,your score only "+_score) //變量賦值 } } studentScore("le","F") } }
2、對類型進行模式匹配(案例:異常處理)
// Scala的模式匹配一個強大之處就在於,可以直接匹配類型,而不是值!!!這點是java的switch case絕對做不到的。 // 理論知識:對類型如何進行匹配?其他語法與匹配值其實是一樣的,但是匹配類型的話,就是要用“case 變量: 類型 => 代碼”這種語法,而不是匹配值的“case 值 => 代碼”這種語法。 // 案例:異常處理 object test{ def main(args: Array[String]): Unit = { import java.io._ def processException(e: Exception) { e match { case e1: IllegalArgumentException => println("you have illegal arguments! exception is: " + e1) case e2: FileNotFoundException => println("cannot find the file you need read or write!, exception is: " + e2) case e3: IOException => println("you got an error while you were doing IO operation! exception is: " + e3) case _: Exception => println("cannot know which exception you have!" ) } } processException(new IOException("not such file")) } }
3、對Array和List的元素進行模式匹配(案例:對朋友打招呼)
// 對Array進行模式匹配,分別可以匹配帶有指定元素的數組、帶有指定個數元素的數組、以某元素打頭的數組 // 對List進行模式匹配,與Array類似,但是需要使用List特有的::操作符 // 案例:對朋友打招呼 object test{ def main(args: Array[String]): Unit = { def greeting(arr: Array[String]) { arr match { case Array("Leo") => println("Hi, Leo!") //匹配一個元素 case Array(girl1, girl2, girl3) => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3) //匹配三個元素 case Array("Leo", _*) => println("Hi, Leo, please introduce your friends to me.") //匹配以Leo開頭,三個元素會被上面匹配scala匹配機制是匹配到就停止 case _ => println("hey, who are you?") } } greeting(Array("Leo","lily","poly","jack")) } } //list object test{ def main(args: Array[String]): Unit = { def greeting(list: List[String]) { list match { case "Leo" :: Nil => println("Hi, Leo!") case girl1 :: girl2 :: girl3 :: Nil => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3) case "Leo" :: tail => println("Hi, Leo, please introduce your friends to me.") case _ => println("hey, who are you?") } } greeting(List("Leo","jack","poly","herry")) } }
4、case class與模式匹配(案例:學校門禁)
// Scala中提供了一種特殊的類,用case class進行聲明,中文也可以稱作樣例類。case class其實有點類似於Java中的JavaBean的概念。即只定義field,並且由Scala編譯時自動提供getter和setter方法,但是沒有method。 // case class的主構造函數接收的參數通常不需要使用var或val修飾,Scala自動就會使用val修飾(但是如果你自己使用var修飾,那么還是會按照var來) // Scala自動為case class定義了伴生對象,也就是object,並且定義了apply()方法,該方法接收主構造函數中相同的參數,並返回case class對象 // 案例:學校門禁 class Person case class Teacher(name:String,subject:String) extends Person case class Student(name:String,classroom:Int) extends Person case class Worker(name:String,work:String) extends Person case class Stranger() extends Person object test{ def main(args: Array[String]): Unit = { def entranceGuard(p:Person): Unit ={ p match { case Student(name,classroom)=>println(s"hello,$name,welcome to school,your classroom is $classroom") case Teacher(name,subject)=>println(s"hello,$name,welcome to school,your teach $subject") case Worker(name,work) if work=="repairman"=>println(s"hello,$name,you should leave school afternoon") case Worker(name,work)=>println(s"hello,$name,you should leave school 2 hours later") case _=>println(s"stranger,you can not into school") } } entranceGuard(Worker("Jason","cleaner")) } }
5、Option與模式匹配(案例:成績查詢)
// Scala有一種特殊的類型,叫做Option。Option有兩種值,一種是Some,表示有值,一種是None,表示沒有值。 // Option通常會用於模式匹配中,用於判斷某個變量是有值還是沒有值,這比null來的更加簡潔明了 // Option的用法必須掌握,因為Spark源碼中大量地使用了Option,比如Some(a)、None這種語法,因此必須看得懂Option模式匹配,才能夠讀懂spark源碼。 object test{ def main(args: Array[String]): Unit = { val grades = Map("Leo" -> "A", "Jack" -> "B", "Jen" -> "C") def getGrade(name: String) { val grade = grades.get(name) grade match { case Some(grade) => println("your grade is " + grade) case None => println("Sorry, your grade information is not in the system") } } getGrade("J") } }