引言
模式匹配是Scala中非常有特色,非常强大的一种功能。模式匹配,其实类似于Java中的swich case语法,即对一个值进行条件判断,然后针对不同的条件,进行不同的处理。
但是Scala的模式匹配的功能比Java的swich case语法的功能要强大地多,Java的swich case语法只能对值进行匹配。但是Scala的模式匹配除了可以对值进行匹配之外,还可以对类型进行匹配、对Array和List的元素情况进行匹配、对case class进行匹配、甚至对有值或没值(Option)进行匹配。
而且对于Spark来说,Scala的模式匹配功能也是极其重要的,在spark源码中大量地使用了模式匹配功能。因此为了更好地编写Scala程序,并且更加通畅地看懂Spark的源码,学好模式匹配都是非常重要的。
模式匹配
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阻止)
class Moshi {
def judgGrade(grade :String): Unit ={
grade match {
case "A" => println("very good")
case "B" => println("good")
case "C" => println("just so so")
case _ => println("you need work harder")
}
}
}
object TextDemo {
def main(args: Array[String]): Unit = {
//************16********1******模式匹配
val a = new Moshi
a.judgGrade("r") //you need work harder
}
}
在模式匹配中使用if守卫
Scala的模式匹配语法,有一个特点在于,可以在case后的条件判断中,不仅仅只是提供一个值,而是可以在值后面再加一个if守卫,进行双重过滤。
class Demo_16_2 {
def judgeGrad(name :String, grad:String): Unit ={
grad match {
case "A" => println(name + "you are excellent")
case "B" => println(name + "you are good")
case "C" => println(name + "yu are just so so")
case _ if name == "leo" => println(name + "you are a good boy")
case _ => println("you need to work harder")
}
}
}
object TextDemo {
def main(args: Array[String]): Unit = {
//************16********2****** 在模式匹配中使用if守卫
val a = new Demo_16_2
a.judgeGrad("leo","E") //leoyou are a good boy
a.judgeGrad("tom","B") //tomyou are good
a.judgeGrad("tom","E") //you need to work harder
}
}
Scala的模式匹配语法,有一个特点在于,可以将模式匹配的默认情况下将下划线,替换为一个变量名,此时模式匹配语法就会将要匹配的值赋值给这个变量,从而可以在后面的处理语句中使用要匹配的值。
但是对于下划线_这种情况,所有不满足前面的case的值,都会进入_这种默认情况进行处理,此时如果我们在处理语句中需要拿到具体的值进行处理呢?那就需要使用这种在模式匹配中进行变量赋值的语法!!
class Demo_16_3 {
def judgeGrad(name :String, grad:String): Unit ={
grad match {
case "A" => println(name + "you are excellent")
case "B" => println(name + "you are good")
case "C" => println(name + "yu are just so so")
case baa => println("you grad is " + baa + " work harder")
}
}
}
object TextDemo {
def main(args: Array[String]): Unit = {
//************16********3****** 在模式匹配中使用if守卫
val a = new Demo_16_3
a.judgeGrad("tom","E") //you grad is E work harder
}
}
对类型的匹配
object TextDemo {
def main(args: Array[String]): Unit = {
def processException(e : Exception): Unit ={
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 IllegalArgumentException) //you have illegal arguments! exception is: java.lang.IllegalArgumentException
processException(new NullPointerException) //cannot know which exception you have!
}
}
对Array和List进行模式匹配
对Array进行模式匹配,分别可以匹配带有指定元素的数组、带有指定个数元素的数组、以某元素打头的数组。 对List进行模式匹配,与Array类似,但是需要使用List特有的“::”操作符。
Array
object TextDemo {
def main(args: Array[String]): Unit = {
//************16********5******对Array和List进行模式匹配
def greeding_16_5(arr :Array[String]): Unit ={
arr match {
case Array("leo") => println("Hi , leo")
case Array(boy1,boy2,boy3) => println("Hi ,boy,nice to meet you. " + " and "+boy1+" and "+boy2+" and "+boy3)
case Array("leo",_*) => println("Hi, Leo, please introduce your friends to me." + arr.foreach(println(_)))
case _a => println("hey, who are you?" + _a.foreach(println(_)))
}
}
// val a = Array("leo")
// greeding_16_5(a) //Hi , leo
// val b = Array("tom")
// greeding_16_5(b) //tom hey, who are you?()
// val c = Array("wangzhaojun","xishi","diaochan")
// //greeding_16_5(c) //Hi ,boy,nice to meet you. and wangzhaojun and xishi and diaochan
// val d = Array("wangzhaojun","xishi","diaochan","yangyuhuan")
// greeding_16_5(d)
//wangzhaojun
//xishi
//diaochan
//yangyuhuan
//hey, who are you?()
List
object TextDemo {
def main(args: Array[String]): Unit = {
//************16********5******对Array和List进行模式匹配
def greeting_15_5_2(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." + list.foreach(println(_)))
case _ => println("hey, who are you?")
}
}
// val l = List("Leo")
// greeting_15_5_2(l) //Hi, Leo!
val f = List("Leo","zhangfei","kangkang")
//greeting_15_5_2(f) //Hi, girls, nice to meet you. Leo and zhangfei and kangkang
val f2 = List("Leo","ton","tom","joker","tyler","join")
greeting_15_5_2(f2)
//Leo
//ton
//tom
//joker
//tyler
//join
//Hi, Leo, please introduce your friends to me.()
}
}
Option与模式匹配
Scala有一种特殊的类型,叫做Option。Option有两种值,一种是Some,表示有值,一种是None,表示没有值。Option通常会用于模式匹配中,用于判断某个变量是有值还是没有值,这比null来的更加简洁明了。
object Demo2 {
val grades = Map("Leo" -> "A", "Jack" -> "B", "Jen" -> "C")
def getGrade_16_7(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")
}
}
def main(args: Array[String]): Unit = {
//-------16----------7--Option与模式匹配
getGrade_16_7("Jack") //your grade is B
//getGrade_16_7("Tom") //Sorry, your grade information is not in the system
}
}