一、基本概念及举例
1、scala中的match case(模式匹配),类似于java中的switch case
2、scala中的模式匹配,可以匹配变量的类型、集合的元素、有无值等各种情况;java中的switch case 却只能匹配数值;
3、语法:变量 match{case 值 => 代码 ... case _ =>代码}, case _ 相当于java中switch case 的default;
4、在scala的模式匹配中,只要一个case分支满足了条件就会自动阻止;java中switch case 却需要break阻止;
val grade:String="A"
def judgeGrade(grade:String): Unit ={
grade match {
case "A" => println("Best")
case "B" => println("Good")
case _ => println("bad")
}
}
judgeGrade(grade)
Best
二、模式匹配中使用if匹配
def judgeGrade01(grade:String,name:String): Unit ={
grade match {
case "A" => println("Best")
case "B" => println("Good")
case _ if name=="LiSi" => println("LiSi Come on")
case _ => println("bad")
}
}
judgeGrade01("V","LiSi")
judgeGrade01("A","LiSi")
LiSi Come on
Best
三、对类型进行模式匹配
语法:变量 match {case 变量: 类型 => 代码}
val num:Int=88
val str:String="zbc"
val oth:Float=99
def judgeType(t:Any)=t match {
case t:String => "String"
case t:Int => "Int"
case t:Double => "Double"
case _ => "Other Type"
}
println(judgeType(num))
println(judgeType(str))
println(judgeType(oth))
Int
String
Other Type
四、对数组和List进行模式匹配
可以匹配带有指定元素的数组和List、带有指定个数元素的数组和List、以某元素开头的数组和List
def matchArr(arr:Array[String]): String ={
arr match{
case Array("A") => "Hello A"
case Array("A","B") =>"Hello A、B"
case Array("A",_*) => "Hello and Other Str"
case _ => "What str is it?"
}
}
println(matchArr(Array("A")))
println(matchArr(Array("A","B")))
println(matchArr(Array("A","B","E")))
println(matchArr(Array("B")))
println(matchArr(Array("E")))
Hello A
Hello A、B
Hello and Other Str
What str is it?
What str is it?
def matchList(list:List[String]):String={
list match {
case "A"::Nil => "Hello A"
case "A"::"B"::Nil => "Hello A、B"
case "A"::tail => "Hello and Other Str"
case _ => "What str is it?"
}
}
println(matchList(List("A")))
println(matchList(List("A","B")))
println(matchList(List("A","B","E")))
println(matchList(List("B")))
println(matchList(List("E")))
Hello A
Hello A、B
Hello and Other Str
What str is it?
What str is it?
五、模式匹配与case class
1、scala中的case class类似于Java中的JavaBean。即只定义field,并且由Scala编译时自动提供getter和setter方法,但是没有方法。
2、Scala自动为case class定义了伴生对象,也就是object,并且定义了apply()方法,该方法接收主构造函数中相同的参数,并返回case class对象
package com.scala.test.Test
import com.scala.test.Single.Person
class Person
case class Teacher(name:String,subject:String) extends Person
case class Student(name:String,classroom:String) extends Person
object MatchTest {
def main(args: Array[String]): Unit = {
def judgePerson(p:Person):String={
p match{
case Teacher(name,subject)=>"Teacher: name is "+name+" subject is "+subject
case Student(name,classroom) => "Student: name is "+name+" classroom is "+classroom
case _ => "No this Persion"
}
}
println(judgePerson(Teacher("Wang","English")))
println(judgePerson(Student("Liu","009")))
}
}
Teacher: name is Wang subject is English
Student: name is Liu classroom is 009
六、Option与模式匹配
1、 Scala有一种特殊的类型,叫做Option。Option有两种值,一种是Some,表示有值,一种是None,表示没有值。
2、Option通常会用于模式匹配中,用于判断某个变量是有值还是没有值。
3、Spark源码中大量地使用了Option,比如Some(a)、None这种语法,因此必须看得懂Option模式匹配,才能够读懂spark源码。
val map = Map("A"-> "aa","B"->"bb","C"->"cc")
def matchOption(key:String)={
val value= map.get(key);
value match{
case Some(v)=>v
case None =>"no this value"
}
}
println(matchOption("A"))
println(matchOption("E"))
aa
no this value
七、元组与模式匹配
val tuple3=("spark","scala","storm")
val tuple2=("spark","scala")
def matchTuple(t:Any): Any ={
t match{
case (one,_,_)=>one
case _ => "Other"
}
}
println(matchTuple(tuple3))
println(matchTuple(tuple2))
spark
Other