DT梦工厂 第27讲 Type,Array,List,Tuple模式匹配实战解析

王家林亲授《DT大数据梦工厂》大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频、PPT、代码下载:
百度云盘:http://pan.baidu.com/s/1c0noOt6 
腾讯微云:http://url.cn/TnGbdC 
360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2
土豆:http://www.tudou.com/programs/view/dHz5JKJxurM/
优酷:http://v.youku.com/v_show/id_XMTI4OTcwNzY2MA==.html?from=s1.8-1-1.2
爱奇艺:http://www.iqiyi.com/w_19rru5bi79.html#vfrm=2-3-0-1
腾讯视频:http://v.qq.com/boke/page/k/0/d/k016008s0rd.html
技术爱好者尤其是大数据爱好者 可以加DT大数据梦工厂的qq群

DT大数据梦工厂① :462923555 
DT大数据梦工厂②:437123764 
DT大数据梦工厂③ :418110145

微信公众账号: DT_Spark
王家林老师微信号: 18610086859
王家林老师QQ: 1740415547
王家林老师邮箱: 18610086859@126.com

本视频由王家林老师, 亲自讲解, 完全通过代码实战把您带人大数据的时代.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package  com.dt.scala.pattern _ match
 
/**
  * @author iken
  * @date 2015-08-29
  */
object  PatternMatchMore {
   def  main( args  :  Array[String] ){
     
     /*
      * 1. 对一个未知类型的变量匹配到相应的类型上去
      *    这是如此的高级,模式匹配可以在程序运行的时候,智能判断出被匹配的内容的类型
      *    注意:Map没有指定其类型,原因是scala在进行模式匹配时,对于接口和泛型,scala会擦出具体的类型
      *    所以使用了占位符
      */
     def  match _ type( t  :  Any )  =  match {
       case  p :  Int  = > println( "It is a integer!" )
       case  p :  String  = > println( "It is a Integer!" )
       case  m :  Map[ _ , _ = > m.foreach(println)
       case  _  = >  println( "unknow type!" )
     }
     
     match _ type( 2 )
     match _ type(Map( "Scala"  ->  "Spark" ))
       
     /*
      * 2.对一个未知的数组进行匹配,它可以将数组匹配到具体形式的case上
      *   case1中是说这个数组只有一个元素,而且该元素必须是0
      *   case2中是说这个数组具有两个任意元素
      *   case3中是说这个数据具有任意多元素,且第一个元素必须是0
      */
     def  match _ array( arr  :  Any )  =  arr  match {
       case  Array( 0 = > println( "Array" + "0" )
       case  Array(x,y)  = > println( "Array " +x+ " " +y)
       case  Array( 0 , _ *)  = > println( "Array" + "0 ..." )
       case  _  = >  println( "some thing!" )
     }
     
     match _ array(Array( 0 ))
     match _ array(Array( 0 , 1 ))
     match _ array(Array( 0 , 1 , 2 , 3 , 4 ))
     match _ array(Array( "hello" , "world" ))
     
     /*
      * 3.对一个未知的List进行匹配,它可以将数组匹配到具体形式的case上
      *   case1中是说这个List只有一个元素,而且该元素必须是0
      *   case2中是说这个List具有两个任意元素
      *   case3中是说这个List具有任意多元素,且第一个元素必须是0
      */
     def  match _ list( lst  :  Any )  =  lst  match {
       case  0  ::  Nil  = > println( "List" + "0" )
       case  ::  ::  Nil  = > println( "List " +x+ " " +y)
       case  0  ::  tail  = > println( "List" + "0 ..." )
       case  _  = >  println( "some thing!" )
     }
     
     match _ list(List( 0 ))
     match _ list(List( 0 , 1 ))
     match _ list(List( 0 , 1 , 2 , 3 , 4 ))
     match _ list(List( "hello" , "world" ))
     
      /*
      * 4.对一个未知的Tuple进行匹配,它可以将数组匹配到具体形式的case上
      *   case1中是说这个Tuple只有一个元素,而且该元素必须是0
      *   case2中是说这个Tuple具有两个任意元素
      *   case3中是说这个Tuple具有任意多元素,且第一个元素必须是0
      */  
     def  match _ tuple( tuple  :  Any )  =  tuple  match {
       case  ( 0 , _ = > println( "List" + "0" )
       case  (x, _ = > println( "List " +x)
       case  _  = >  println( "some thing!" )
     }
     match _ tuple(( 0 , "scala" ))
     match _ tuple(( "hello" , 1 ))
     match _ tuple(( 0 , 1 , 2 , 3 , 4 ))
   }
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值