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

来源:互联网 发布:安卓流程图软件 编辑:程序博客网 时间:2024/05/08 10:09

王家林亲授《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))
  }
}

  

0 0
原创粉丝点击