码迷,mamicode.com
首页 > 其他好文 > 详细

Scala实战高手****第13课Scala模式匹配实战和Spark源码鉴赏

时间:2018-11-21 18:37:09      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:cas   spark   boolean   int   put   结果   class   att   hadoop   

package com.dt.spark.scala.bascis
 
class Dataframework 
case class Computerframework (name:String,popular:Boolean) extends Dataframework 
case class Storgeframework (name:String,popular:Boolean) extends Dataframework 
 
object helloPatternMatch {
  def main(args: Array[String]): Unit = {
   // getSalary("hadoop")
   // getSalary("flink")
   // getSalary("scala")
    
    getSalary("scalasafdas",6)
    getMatchType(100.0)
    getMatchType("java")
    getValue("spark",Map("spark"->"the hosttest!!"))
    
    getBigDataType(Computerframework("spark",true))
 
    getMatchTypeCollection(Array("scala"))
    
  }
  
  def getSalary(name:String,age:Int){
    name match{
      case "spark" => println("$ 15k")
      case "hadoop" =>  println("$ 12k")
      case _ if name == "scala" =>  println("$ 14k")
      case _ if name == "flink" =>  println("$ 13k")
      case _name if age >= 5 =>  println(" name : "+ _name +"  $ 16k")
     
      case _ =>  println("$ 8k")
    }
  }
  
  def getMatchType(msg:Any){
    msg match {
      case i: Int => println("integer")
      case s:String => println("String")
       case s:Double => println("Double")
       case array:Array[Int]=>  println("Array")
       case _ => println("other type")
    } 
  }
  
   def getMatchTypeCollection(msg:Array[String]){
    msg match {
      case   Array("scala") => println("1   element")
      case  Array("scala","java")=> println("2  element")
       case  Array("spark",_*) => println("many element")
       case _ => println("other element")
    } 
  }
  
 def getBigDataType(data:Dataframework){
   data match {
     case Computerframework(name,popular) =>  println("name:  "+name+" "+ popular)
     case Storgeframework(name,popular) =>   println(name+" "+ popular)
     case _ =>   println("other types")
   }
   
   
   
 }
   
  def getValue(key:String,content: Map[String,String]){
    content.get(key) match {
      case Some(value) => println(value)
      case None => println("none")
    }
  }
  
  
}
 
 
 
  结果
  
  
  
   name : scalasafdas  $ 16k
Double
String
the hosttest!!
name:  spark true
1   element

  

Scala实战高手****第13课Scala模式匹配实战和Spark源码鉴赏

标签:cas   spark   boolean   int   put   结果   class   att   hadoop   

原文地址:https://www.cnblogs.com/sunrunzhi/p/9996727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!