Scala has a very powerful pattern matching mechanism that can be applied to many occasions: such as switch statements, type checking, etc. Similar to Java's switch statement, but more powerful
Syntax val a mathc{
case xxxx = > xxx
case xxxx=> xxx}
And Scala also provides sample classes that optimize pattern matching for fast matching
1. Match the string
import scala.util.Random object CaseDemo01 extends App{ val arr = Array("Hadoop", "HBase", "Spark") val name = arr(Random.nextInt(arr.length)) name match { case "Hadoop" => println("哈肚普...") case "HBase" => println("H贝斯...") case _ => println("I really don't know what you are talking about...")//similar to default }}
2. Match type
import scala.util.Random object CaseDemo01 extends App{ //val v = if(x >= 5) 1 else if(x < 2) 2.0 else "hello" val arr = Array("hello", 1, 2.0, CaseDemo01) val v = arr(Random.nextInt(4)) println(v) v match { case x: Int => println("Int " + x) case y: Double if(y >= 0) => println("Double "+ y) case z: String => println("String " + z) case _ => throw new Exception("not match exception") } }
3. Match arrays, tuples, sets
object CaseDemo03 extends App{ val arr = Array(1, 3, 5) arr match { case Array(1, x, y) => println(x + " " + y) case Array(0) => println("only 0") case Array(0, _*) => println("0 ...") case _ => println("something else") } val lst = List(3, -1) lst match { case 0 :: Nil => println("only 0") case x :: y :: Nil => println(s"x: $x y: $y") case 0 :: tail => println("0 ...") case _ => println("something else") } val tup = (2, 3, 5) tup match { case (2, x, y) => println(s"1, $x , $y") case (_, z, 5) => println (z) case _ => println("else") } }
4. Sample class case class
A sample class is a special class in Scala that can be used for pattern matching. case class is multi-instance, followed by construction parameters, case object is singleton
import scala.util.Random case class SubmitTask(id: String, name: String) case class HeartBeat(time: Long) case object CheckTimeOutTask object CaseDemo04 extends App{ val arr = Array(CheckTimeOutTask, HeartBeat(12333), SubmitTask("0001", "task-0001")) arr(Random.nextInt(arr.length)) match { case SubmitTask(id, name) => { println(s"$id, $name")//String combination } case HeartBeat(time) => { println(time) } case CheckTimeOutTask => { println("check") } } }
The sample class is mainly used for pattern matching, used in RPC, the case is here
Essentially, a class case class works the same way:
The most different thing: if we want to do pattern matching in scala and match types, it is recommended to use
The case case is optimized by the bottom layer of scala , and the matching performance is better.* 1: The case class automatically generates a companion object, and automatically implements the apply method
. * 2: The case class is used for matching and has better performance (the bottom layer of scala has been optimized)
* 3: The case class implements Serializable by default
* 4: The case class implements methods such as toString equals by default
* 5 : There is no modifier in the main constructor of case class , the default is val
partial function
A set of case statements without match enclosed in curly braces is a partial function, which is an instance of PartialFunction[A, B], where A represents the parameter type, B represents the return type, and the match keyword is omitted compared to the regular, Often used for input pattern matching
def func1: PartialFunction[String, Int] = { case "one" => 1 case "two" => 2 case _ => -1 }