Scala pattern matching five scenarios, values, case class, optional, data types, data in the collection __scala

Source: Internet
Author: User
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 ')}}}
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.