Scala的集合框架类比Java提供了更多的一些方便的api,使得使用scala编程时代码变得非常精简,尤其是在Spark中,很多功能都是由scala的这些api构成的,所以,了解这些方法的使用,将更加有助于我们学习Scala和Spark: 
List,Map,Set的一些api的用法如下: 

  1. /**
  2. * Scala 集合测试
  3. */
  4. def collectionTest(): Unit ={
  5. val map =Map("red"->"红色","green"->"绿色")
  6. val colors = List("red", "green", "blue")
  7. //map函数测试 ,打印长度
  8. val size=colors.map( (f:String) => f.size  );
  9. val data=List(1,2,5,3);
  10. //reduce函数测试 求和
  11. val r=data.reduce((a:Int,b:Int)=>a+b);
  12. println(size,"            ",r)
  13. //打印取值
  14. println(map("red"))
  15. //判断是否存在
  16. println(map.contains("white"))
  17. //遍历map集合
  18. for(pair<-map) println(pair)
  19. //去重打印
  20. println(List(3,2,3,4,5).distinct)
  21. //fitler过滤
  22. println(List(3,12,33,64,15).filter(_ > 18))
  23. //扁平化处理
  24. println(List(List(1, 2), List(3, 4)).flatten)
  25. //partition 分区
  26. println(List(1, 2, 3, 4, 5) partition (_ < 3))
  27. //反转集合
  28. println(List(1, 2, 3).reverse)
  29. //slice==>与java的substring类似
  30. println(List(2, 3, 5, 7) slice (1, 4))
  31. //排序sortBy
  32. println(List("apple", "to","a","ab") sortBy (_.size) )
  33. //排序原生值
  34. println(List("apple", "to","bag","bbc","one").sorted)
  35. //splitAt值拆分一个list,根据下标的位置
  36. println(List(2, 3, 5, 7,99,45) splitAt 3)
  37. //提取第n个元素之前数据作为一个新的集合
  38. println(List(2, 3, 5, 7, 11, 13) take 2)
  39. //合并两个集合,进入一个大的集合
  40. println(List(1, 2) zip List("a", "b") )
  41. //删除前n个元素后,新生成一个集合
  42. println(List('a', 'b', 'c', 'd') drop 1)
  43. //判断两个集合是否相等
  44. println(List(1, 2) == List(1, 2))
  45. //合并两个集合 ++
  46. println(List(1, 2) ++ Set(3, 4, 3) )
  47. //合并两个集合
  48. println(List(1, 2) ::: List(2, 3))
  49. //添加一个元素
  50. println(1 :: 2 :: Nil)
  51. //添加一个元素
  52. println(List(1,6).:+(5))
  53. //遍历每一个元素,收集case匹配上的元素值,返回一个新的集合
  54. println(List(0, 1, 0) collect {case 1 => "ok"})
  55. //将一个元素,根据特定符号,拆分成单个元素组成的集合
  56. println(List("milk,tea") flatMap (_.split(',')))
  57. //最大值
  58. println(List(41, 59, 26).max)
  59. //最小值
  60. println(List(10.9, 32.5, 4.23, 5.67).min)
  61. //连乘
  62. println(List(5, 6, 10).product)
  63. //求和
  64. println(List(11.3, 23.5, 7.2).sum )
  65. //是否存在
  66. println(List(34, 29, 18) contains 29 )
  67. //是否以xxx结束
  68. println(List(0, 4, 3) endsWith List(4, 3))
  69. //是否以xxx开始
  70. println(List(0, 4, 3) startsWith List(0) )
  71. //最少有一个值小于18即为true
  72. println(List(24, 17, 32) exists (_ < 18))
  73. println("=======================================")
  74. //必须得所有值小于18才返回true
  75. println(List(5, 17, 2) forall (_ < 18) )
  76. //求和
  77. println(List(4, 5, 6).fold(8)(_ + _))
  78. //求和
  79. println(List(4, 5, 6).foldLeft(10)(_ + _))
  80. //求和
  81. println(List(4, 5, 6).foldRight(0)(_ + _) )
  82. //求和
  83. println(List(4, 5, 6).reduce(_ + _) )
  84. //求和
  85. println(List(4, 5, 6).reduceLeft(_ + _) )
  86. //求和
  87. println(List(4, 5, 6).reduceRight(_ + _) )
  88. //阶段求和
  89. println(List(4, 5, 6).scan(0)(_ + _))
  90. //阶段求和
  91. println(List(4, 5, 6).scanLeft(0)(_ + _))
  92. //阶段求和
  93. println(List(4, 5, 6).scanRight(0)(_ + _))
  94. //集合转换字符串
  95. println(List(24, 99, 104).mkString(", "))
  96. //转成ArrayBuffer
  97. println(List('f', 't').toBuffer)
  98. //转Map to List
  99. println(Map("a" -> 1, "b" -> 2).toList )
  100. //转Set to Map
  101. println(Set(1 -> true, 3 -> true).toMap)
  102. //转List to Set
  103. println(List(2, 5, 5, 3, 2).toSet)
  104. //转List to String
  105. println(List(2, 5, 5, 3, 2).toString)
  106. //Java 与 Scala 集合互转
  107. //导入包
  108. import  collection.JavaConverters._
  109. import  collection.JavaConversions._
  110. import java.util.ArrayList;
  111. // scala 转 java
  112. val ja  =List(1,5,3).asJava
  113. println(List(1,5,3))
  114. println(ja)
  115. // java 转 scala
  116. val s =new java.util.ArrayList(3).asScala;
  117. //集合的模式匹配
  118. val statuses = List(500, 404)
  119. val msg = statuses.head match {
  120. case x if x < 500 => "okay"
  121. case _ => "whoah, an error"
  122. }
  123. println("信息:",msg)
  124. val msg2 = statuses match {
  125. case x if x contains(500) => "has error"
  126. case _ => "okay"
  127. }
  128. println("信息:",msg2)
  129. val msg3 = statuses match {
  130. case List(404, 500) => "not found & error"
  131. case List(500, 404) => "error & not found"
  132. case List(200, 200) => "okay"
  133. case _ => "not sure what happened"
  134. }
  135. println("信息:",msg3)
  136. val msg4 = statuses match {
  137. case List(500, x) => s"Error followed by $x"
  138. case List(e, x) => s"$e was followed by $x"
  139. }
  140. println("信息:",msg4)
  141. val head = List('r','g','b') match {
  142. case x :: xs => x
  143. case Nil => ' '
  144. }
  145. println(head)
  146. val code = ('h', 204, true) match {
  147. case (_, _, false) => 501
  148. case ('c', _, true) => 302
  149. case ('h', x, true) => x
  150. case (c, x, true) => {
  151. println(s"Did not expect code $c")
  152. x
  153. }
  154. }
  155. println(code)
  156. }


顺序输出结果如下: 

    1. (List(3, 5, 4),            ,11)
    2. 红色
    3. false
    4. (red,红色)
    5. (green,绿色)
    6. List(3, 2, 4, 5)
    7. List(33, 64)
    8. List(1, 2, 3, 4)
    9. (List(1, 2),List(3, 4, 5))
    10. List(3, 2, 1)
    11. List(3, 5, 7)
    12. List(a, to, ab, apple)
    13. List(apple, bag, bbc, one, to)
    14. (List(2, 3, 5),List(7, 99, 45))
    15. List(2, 3)
    16. List((1,a), (2,b))
    17. List(b, c, d)
    18. true
    19. List(1, 2, 3, 4)
    20. List(1, 2, 2, 3)
    21. List(1, 2)
    22. List(1, 6, 5)
    23. List(ok)
    24. List(milk, tea)
    25. 59
    26. 4.23
    27. 300
    28. 42.0
    29. true
    30. true
    31. true
    32. true
    33. =======================================
    34. true
    35. 23
    36. 25
    37. 15
    38. 15
    39. 15
    40. 15
    41. List(0, 4, 9, 15)
    42. List(0, 4, 9, 15)
    43. List(15, 11, 6, 0)
    44. 24, 99, 104
    45. ArrayBuffer(f, t)
    46. List((a,1), (b,2))
    47. Map(1 -> true, 3 -> true)
    48. Set(2, 5, 3)
    49. List(2, 5, 5, 3, 2)
    50. List(1, 5, 3)
    51. [1, 5, 3]
    52. (信息:,whoah, an error)
    53. (信息:,has error)
    54. (信息:,error & not found)
    55. (信息:,Error followed by 404)
    56. r
    57. 204
    58. Process finished with exit code 0

Scala集合笔记的更多相关文章

  1. Scala 学习笔记之集合(3)

    建立一个Java类,为了演示Java集合类型向Scala集合的转换: import java.util.ArrayList; import java.util.List; public class S ...

  2. scala 学习笔记--集合

    1.scala集合的null 是nil 而不是null 2.set的三个方法union,intersect,diff union--合并去重 intersect--交集 diff--a减去(a和b交集 ...

  3. 【大数据】Scala学习笔记

    第 1 章 scala的概述1 1.1 学习sdala的原因 1 1.2 Scala语言诞生小故事 1 1.3 Scala 和 Java  以及 jvm 的关系分析图 2 1.4 Scala语言的特点 ...

  4. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  5. Scala编程 笔记

    date: 2019-08-07 11:15:00 updated: 2019-11-25 20:00:00 Scala编程 笔记 1. makeRDD 和 parallelize 生成 RDD de ...

  6. Scala集合操作

    大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka.  ...

  7. Spark:scala集合转化为DS/DF

    scala集合转化为DS/DF case class TestPerson(name: String, age: Long, salary: Double) val tom = TestPerson( ...

  8. Scala语言笔记 - 第一篇

    目录 Scala语言笔记 - 第一篇 1 基本类型和循环的使用 2 String相关 3 模式匹配相关 4 class相关 5 函数调用相关 Scala语言笔记 - 第一篇 ​ 最近研究了下scala ...

  9. Scala集合常用方法解析

    Java 集合 : 数据的容器,可以在内部容纳数据  List : 有序,可重复的  Set : 无序,不可重复  Map : 无序,存储K-V键值对,key不可重复 scala 集合 : 可变集合( ...

随机推荐

  1. noi.openjudge 2.6.162 Post Office

    http://noi.openjudge.cn/ch0206/162/ 总时间限制:  1000ms 内存限制:  65536kB 描述 There is a straight highway wit ...

  2. vs2010中使用 git

    在没有使用git之前的,我很苦恼.因为我的代码有时在办公室做,有时也带回家做.做了一些时间,放在哪,要用的时间就不知道家里的还是办公室的是新版本了.甚至出现了旧版本把新版本覆盖的乌龙事情.有了git只 ...

  3. CalISBN.java

    /****************************************************************************** * Compilation: javac ...

  4. js jquery获取当前元素的兄弟级 上一个 下一个元素

    原博地址:http://www.jb51.net/article/71782.htm var chils= s.childNodes;  //得到s的全部子节点 var par=s.parentNod ...

  5. CM记录-操作系统调优

    1.避免使用swap分区---将hadoop守护进程的数据交换到磁盘的行为可能会导致操作超时:物理内存(交换)--Swap分区 2.调整内存分配策略---操作系统内核根据vm.overcommit_m ...

  6. Nginx实现数据库端口转发

    前言 因开发.测试.生成等服务器网络策略问题,导致部分服务器A需要访问数据库而无法正常访问数据库,此处采用端口代理方式解决此问题,即通过一台能正常访问数据库的服务器B做tcp端口代理,实现服务器A通过 ...

  7. This page is about building Firefox Desktop

    This page is about building Firefox Desktop The Mozilla build system, like the rest of the Mozilla c ...

  8. luogu 1030 求先序遍历

    此题给出中序遍历和后序遍历后的序列,乍一看确乎想不出法子解决,毕竟是逆向思维: 但是后序遍历的特殊性质 son1,son2,x 使得后序的末尾便是根节点,再由中序天然的递归性质便可递归输出,用下fin ...

  9. Java EE之Struts2路径访问小结

    一.项目WEB视图结构 注释:struts.xml:最普通配置,任何无特殊配置 二.访问页面 1.访问root.jsp //方式1: http://localhost/demo/root.jsp // ...

  10. MGR架构~单写模式架构的搭建

    一 简介 :MGR一直没有时间测试,今天咱们来初步了解搭建一下呗 二 环境: mysql5.7.20  单台机器 启动三实例 三  mysql 搭建: 1 建立相关目录+ mkdir -p /data ...