1、模式匹配1java
一、介紹數組
模式匹配是Scala中很是有特點,很是強大的一種功能。模式匹配,其實相似於Java中的swich case語法,即對一個值進行條件判斷,而後針對不一樣的條件,
進行不一樣的處理。
可是Scala的模式匹配的功能比Java的swich case語法的功能要強大地多,Java的swich case語法只能對值進行匹配。可是Scala的模式匹配除了能夠對值進行匹配以外,
還能夠對類型進行匹配、對Array和List的元素狀況進行匹配、對case class進行匹配、甚至對有值或沒值(Option)進行匹配。
並且對於Spark來講,Scala的模式匹配功能也是極其重要的,在spark源碼中大量地使用了模式匹配功能。所以爲了更好地編寫Scala程序,而且更加通暢地看懂Spark的
源碼,學好模式匹配都是很是重要的。
二、模式匹配基礎語法app
// Scala是沒有Java中的switch case語法的,相對應的,Scala提供了更增強大的match case語法,即模式匹配,類替代switch case,match case也被稱爲模式匹配
// Scala的match case與Java的switch case最大的不一樣點在於,Java的switch case僅能匹配變量的值,比一、二、3等;而Scala的match case能夠匹配各類狀況,好比變量的
類型、集合的元素、有值或無值
// match case的語法以下:變量 match { case 值 => 代碼 }。若是值爲下劃線,則表明了不知足以上全部狀況下的默認狀況如何處理。此外,match case中,
只要一個case分支知足並處理了,就不會繼續判斷下一個case分支了。(與Java不一樣,java的switch case須要用break阻止)
// match case語法最基本的應用,就是對變量的值進行模式匹配
//案例:成績評價
scala> :paste
// Entering paste mode (ctrl-D to finish)
def judgeGrade(grade: String) {
grade match {
case "A" => println("Excellent")
case "B" => println("Good")
case "C" => println("Just so so")
case _ => println("you need to work harder")
}
}
// Exiting paste mode, now interpreting.
judgeGrade: (grade: String)Unit
scala> judgeGrade("A")
Excellent
scala> judgeGrade("C")
Just so so
scala> judgeGrade("D")
you need to work harder
三、在模式匹配中使用if守衛socket
// Scala的模式匹配語法,有一個特色在於,能夠在case後的條件判斷中,不只僅只是提供一個值,而是能夠在值後面再加一個if守衛,進行雙重過濾
// 案例:成績評價(升級版)
scala> :paste
// Entering paste mode (ctrl-D to finish)
def judgeGrade(name: String, grade: String) {
grade match {
case "A" => println(name + ", your are really excellent!")
case "B" => println(name + ", you are very good!")
case "C" if name == "leo" => println(name + ",your parents are very hard, please come on!")
case "C" if name == "jack" => println(name + ", your are smart, come on!")
case "C" => println(name + ", Just so so")
case _ => println(name + ", you need to work harder!")
}
}
// Exiting paste mode, now interpreting.
judgeGrade: (name: String, grade: String)Unit
scala> judgeGrade("Jen","A")
Jen, your are really excellent!
scala> judgeGrade("leo","C")
leo,your parents are very hard, please come on!
scala> judgeGrade("jack","C")
jack, your are smart, come on!
scala> judgeGrade("marry","C")
marry, Just so so
四、在模式匹配中進行變量賦值函數
// Scala的模式匹配語法,有一個特色在於,能夠將模式匹配的默認狀況,下劃線,替換爲一個變量名,此時模式匹配語法就會將要匹配的值賦值給這個變量,
從而能夠在後面的處理語句中使用要匹配的值
// 爲何有這種語法??思考一下。由於只要使用case匹配到的值,是否是咱們就知道這個值啦!!在這個case的處理語句中,是否是就直接可使用寫程序時
就已知的值!
// 可是對於下劃線_這種狀況,全部不知足前面的case的值,都會進入_這種默認狀況進行處理,此時若是咱們在處理語句中須要拿到具體的值進行處理呢?那就須要
使用這種在模式匹配中進行變量賦值的語法!!
// 案例:成績評價(升級版)
scala> :paste
// Entering paste mode (ctrl-D to finish)
def judgeGrade(grade: String) {
grade match {
case "A" => println("you got A grade, excellent!")
case "B" => println("you got B grade, good!")
case "C" => println("you got C grade, just so so")
case badGrade => println("you got " + badGrade + " grade, I hope that you can get C grade next time!")
}
}
// Exiting paste mode, now interpreting.
judgeGrade: (grade: String)Unit
scala>
scala> judgeGrade("A")
you got A grade, excellent!
scala> judgeGrade("B")
you got B grade, good!
scala> judgeGrade("D")
you got D grade, I hope that you can get C grade next time!
scala> judgeGrade("E")
you got E grade, I hope that you can get C grade next time!
2、模式匹配2spa
一、對類型進行模式匹配scala
// Scala的模式匹配一個強大之處就在於,能夠直接匹配類型,而不是值!!!這點是java的switch case絕對作不到的。
// 理論知識:對類型如何進行匹配?其餘語法與匹配值實際上是同樣的,可是匹配類型的話,就是要用「case 變量: 類型 => 代碼」這種語法,而不是匹配值
的「case 值 => 代碼」這種語法。
// 案例:異常處理
scala> import java.io._
import java.io._
scala> :paste
// Entering paste mode (ctrl-D to finish)
def processException(e: Exception) {
e match {
case e1: IllegalArgumentException => println("you passed illegal argument. exception is: " + e1)
case e2: FileNotFoundException => println("cannot find the file. exception is " + e2)
case e3: IOException => println("io error occurs. exception is " + e3)
case _: Exception => println("exception occurs.")
}
}
// Exiting paste mode, now interpreting.
processException: (e: Exception)Unit
scala> processException(new IllegalArgumentException("expect two argument, but fount one argument."))
you passed illegal argument. exception is: java.lang.IllegalArgumentException: expect two argument, but fount one argument.
scala> processException(new FileNotFoundException("test.txt not fount."))
cannot find the file. exception is java.io.FileNotFoundException: test.txt not fount.
scala> processException(new IOException("get data from socket fail."))
io error occurs. exception is java.io.IOException: get data from socket fail.
scala> processException(new ArrayIndexOutOfBoundsException("array is null,"))
exception occurs.
二、對Array和List進行模式匹配ssr
// 對Array進行模式匹配,分別能夠匹配帶有指定元素的數組、帶有指定個數元素的數組、以某元素打頭的數組
// 案例:對朋友打招呼
scala> :paste
// Entering paste mode (ctrl-D to finish)
def greeting1(arr: Array[String]) {
arr match {
case Array("Leo") => println("How are you, Leo!")
case Array(girl1, girl2, girl3) => println("Hi, girls, I'm jack, nice to meet you. " + girl1 + ", " + girl2 + ", " + girl3)
case Array("Leo", _*) => println("Hi, Leo, why not introduce your friends to me!")
case stranger => println(stranger + ", hey, who you are!")
}
}
// Exiting paste mode, now interpreting.
greeting1: (arr: Array[String])Unit
scala> greeting
greeting greeting1
scala> greeting1(Array("Leo"))
How are you, Leo!
scala> greeting1(Array("jen", "mary", "lory"))
Hi, girls, I'm jack, nice to meet you. jen, mary, lory
scala> greeting1(Array("Leo", "jack", "mike", "kitty"))
Hi, Leo, why not introduce your friends to me!
scala> greeting1(Array("mike"))
[Ljava.lang.String;@2abd838e, hey, who you are!
// 對List進行模式匹配,與Array相似,可是須要使用List特有的::操做符
scala> :paste
// Entering paste mode (ctrl-D to finish)
def greeting2(list: List[String]) {
list match {
case "leo" :: Nil => println("Hi, leo!")
case girl1 :: girl2 :: girl3 :: Nil => println("Hi girls, may I know your names? " + girl1 + ", " + girl2 + ", " + girl3)
case "leo" :: tail => println("Hi leo, please introduce your friends to me")
case _ => println("hei, who you are?")
}
}
// Exiting paste mode, now interpreting.
greeting2: (list: List[String])Unit
scala> greeting2(List("leo"))
Hi, leo!
scala> greeting2(List("jen", "marry", "lory"))
Hi girls, may I know your names? jen, marry, lory
scala> greeting2(List("leo", "jack"))
Hi leo, please introduce your friends to me
scala> greeting2(List("jack"))
hei, who you are?
三、case class與模式匹配excel
// 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對象
// 案例:學校門禁
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person
case class Teacher(name: String, subject: String) extends Person
case class Student(name: String, classroom: String) extends Person
def judgeIdentify(p: Person) {
p match {
case Teacher(name, subject) => println("Teacher, name is " + name + ", subject you teach is " + subject + "." )
case Student(name, classroom) => println("Student, name is " + name + ", your classroom is " + classroom + ".")
case _ => println("Illegal Access! please go out of the school, or we will call police!")
}
}
// Exiting paste mode, now interpreting.
defined class Person
defined class Teacher
defined class Student
judgeIdentify: (p: Person)Unit
scala> val leo: Person = Student("leo", "class1")
leo: Person = Student(leo,class1)
scala> val tom: Person = Teacher("tom", "Match")
tom: Person = Teacher(tom,Match)
scala> case class Worker(name: String) extends Person
defined class Worker
scala> val jack: Person = Worker("jack")
jack: Person = Worker(jack)
scala> judgeIdentify(leo)
Student, name is leo, your classroom is class1.
scala> judgeIdentify(tom)
Teacher, name is tom, subject you teach is Match.
scala> judgeIdentify(jack)
Illegal Access! please go out of the school, or we will call police!
四、Option與模式匹配code
// Scala有一種特殊的類型,叫作Option。Option有兩種值,一種是Some,表示有值,一種是None,表示沒有值。
// Option一般會用於模式匹配中,用於判斷某個變量是有值仍是沒有值,這比null來的更加簡潔明瞭
// Option的用法必須掌握,由於Spark源碼中大量地使用了Option,好比Some(a)、None這種語法,所以必須看得懂Option模式匹配,纔可以讀懂spark源碼。
// 案例:成績查詢
scala> val grades = Map("Leo" -> "A", "Jack" -> "B", "Tom" -> "C")
grades: scala.collection.immutable.Map[String,String] = Map(Leo -> A, Jack -> B, Tom -> C)
scala> :paste
// Entering paste mode (ctrl-D to finish)
def getGrade(name: String) {
var grade = grades.get(name)
grade match {
case Some(grade) => println("your grade is " + grade)
case None => println("Sorry, your grade is not in the system. please ask your teacher!")
}
}
// Exiting paste mode, now interpreting.
getGrade: (name: String)Unit
scala> getGrade("Leo")
your grade is A
scala> getGrade("Jack")
your grade is B
scala> getGrade("Tom")
your grade is C
scala> getGrade("Marry")
Sorry, your grade is not in the system. please ask your teacher!