Package Com.xing.listfile/** * Created by Dengni on 2017/2/11. */class Dataframework case Class Computationframework (Val name:string,val age:int) extends Dataframework case class Stor
Ageframework (name:string, Age:int) extends Dataframework object Hellopatternmatch {def main (args:array[string]) { Pattern matching pair value matching getsalary ("Spark") getsalary ("Spark1") getsalary ("Spark",) getsalary ("Spark1", 16)/ /pattern matching pairs of data types Getmatchtype () getmatchtype ("Hi") Getmatchtype (12.00) Getmatchtype (Array (1,2)) GETMATC Htype (Array ("string")) Getmatchtype (Seq (12))//Pattern Match getmatchcollection (Array ("Scala")) to the contents of the collection Getmatchco Llection (Array ("Spark")) Getmatchcollection (Array ("Spark", "Scala")) Getmatchcollection (Array ("Java", "SPAKDG"))// Matching to Java after the following will not be matched//pattern matching case class match Getmatchcaseclass (Computationframework ("Hi",) getmatchcaseclass ( Storageframework ("Hello")///getmatchcaseclass ("string") errorCan write another one this extends Data but not shows in the DEF function//pattern match to optional match getmatchoption ("scal A ", Map (" Scala "->" "," Spark "->" Spark ")) getmatchoption (" Java ", Map (" Scala "->" "," Spark "->" Spark "))}/
Pattern matching pair value matching def getsalary (name:string): unit ={name Match {case ' spark ' => println ("High"); Case "Hadoop" => println (' low ') Case _ => println ("Unknown")} def getsalary (name:string,age:int
): Unit ={name Match {case ' spark ' => println (' high ' + age); Case "Hadoop" => println (' low ') Case _ if Age > => println ("maybe" + age) Case _ if age <= 15 => println ("Unknown")}}//pattern match to data type Def getmatchtype (msg:any): Unit ={msg Match {case I:
Int => println ("Integer") Case d:double =>println ("Double") Case s:string =>println ("String") Case A:array[string] =>println ("array[string]") case A:array[int] =>println ("array[int]") Case _ => println ("Unknown Type")}//pattern match match to the contents of the Set Def g
Etmatchcollection (msg:array[string]): Unit ={msg Match {case Array ("Scala") => println ("1 element") Case Array ("Spark", "Scala") => println ("2 elements") Case Array ("Java", _*) => println ("many element S ") Case _ => println (" Unknown ")}//pattern matching case class matching def getmatchcaseclass (Data:dataframewo RK): Unit ={//Incoming is the data type of the parent class, which also explains what to extends data match {case computationframework (name,age) => println ("t The ' is using to computation ')//name and age passed by Mian () Case storageframework (name,age) => println ("the" is For storage ") Case _ => println (" Unknown ")}//pattern matching optional matching def getmatchoption (Name:string,con Tents:map[string,string]: Unit ={//using the Map Key to check the Vale situation Contents.get (name) match {case Some (values) =&G T
println ("exists") Case None => println (' Not Exists ')}}}