Scala的集合框架类比Java提供了更多的一些方便的api,使得使用scala编程时代码变得非常精简,尤其是在Spark中,很多功能都是由scala的这些api构成的,所以,了解这些方法的使用,将更加有助于我们学习Scala和Spark:
List,Map,Set的一些api的用法如下:
Java代码
- /**
- * Scala 集合测试
- */
- def collectionTest(): Unit ={
- val map =Map("red"->"红色","green"->"绿色")
- val colors = List("red", "green", "blue")
- //map函数测试 ,打印长度
- val size=colors.map( (f:String) => f.size );
- val data=List(1,2,5,3);
- //reduce函数测试 求和
- val r=data.reduce((a:Int,b:Int)=>a+b);
- println(size," ",r)
- //打印取值
- println(map("red"))
- //判断是否存在
- println(map.contains("white"))
- //遍历map集合
- for(pair<-map) println(pair)
- //去重打印
- println(List(3,2,3,4,5).distinct)
- //fitler过滤
- println(List(3,12,33,64,15).filter(_ > 18))
- //扁平化处理
- println(List(List(1, 2), List(3, 4)).flatten)
- //partition 分区
- println(List(1, 2, 3, 4, 5) partition (_ < 3))
- //反转集合
- println(List(1, 2, 3).reverse)
- //slice==>与java的substring类似
- println(List(2, 3, 5, 7) slice (1, 4))
- //排序sortBy
- println(List("apple", "to","a","ab") sortBy (_.size) )
- //排序原生值
- println(List("apple", "to","bag","bbc","one").sorted)
- //splitAt值拆分一个list,根据下标的位置
- println(List(2, 3, 5, 7,99,45) splitAt 3)
- //提取第n个元素之前数据作为一个新的集合
- println(List(2, 3, 5, 7, 11, 13) take 2)
- //合并两个集合,进入一个大的集合
- println(List(1, 2) zip List("a", "b") )
- //删除前n个元素后,新生成一个集合
- println(List(‘a‘, ‘b‘, ‘c‘, ‘d‘) drop 1)
- //判断两个集合是否相等
- println(List(1, 2) == List(1, 2))
- //合并两个集合 ++
- println(List(1, 2) ++ Set(3, 4, 3) )
- //合并两个集合
- println(List(1, 2) ::: List(2, 3))
- //添加一个元素
- println(1 :: 2 :: Nil)
- //添加一个元素
- println(List(1,6).:+(5))
- //遍历每一个元素,收集case匹配上的元素值,返回一个新的集合
- println(List(0, 1, 0) collect {case 1 => "ok"})
- //将一个元素,根据特定符号,拆分成单个元素组成的集合
- println(List("milk,tea") flatMap (_.split(‘,‘)))
- //最大值
- println(List(41, 59, 26).max)
- //最小值
- println(List(10.9, 32.5, 4.23, 5.67).min)
- //连乘
- println(List(5, 6, 10).product)
- //求和
- println(List(11.3, 23.5, 7.2).sum )
- //是否存在
- println(List(34, 29, 18) contains 29 )
- //是否以xxx结束
- println(List(0, 4, 3) endsWith List(4, 3))
- //是否以xxx开始
- println(List(0, 4, 3) startsWith List(0) )
- //最少有一个值小于18即为true
- println(List(24, 17, 32) exists (_ < 18))
- println("=======================================")
- //必须得所有值小于18才返回true
- println(List(5, 17, 2) forall (_ < 18) )
- //求和
- println(List(4, 5, 6).fold(8)(_ + _))
- //求和
- println(List(4, 5, 6).foldLeft(10)(_ + _))
- //求和
- println(List(4, 5, 6).foldRight(0)(_ + _) )
- //求和
- println(List(4, 5, 6).reduce(_ + _) )
- //求和
- println(List(4, 5, 6).reduceLeft(_ + _) )
- //求和
- println(List(4, 5, 6).reduceRight(_ + _) )
- //阶段求和
- println(List(4, 5, 6).scan(0)(_ + _))
- //阶段求和
- println(List(4, 5, 6).scanLeft(0)(_ + _))
- //阶段求和
- println(List(4, 5, 6).scanRight(0)(_ + _))
- //集合转换字符串
- println(List(24, 99, 104).mkString(", "))
- //转成ArrayBuffer
- println(List(‘f‘, ‘t‘).toBuffer)
- //转Map to List
- println(Map("a" -> 1, "b" -> 2).toList )
- //转Set to Map
- println(Set(1 -> true, 3 -> true).toMap)
- //转List to Set
- println(List(2, 5, 5, 3, 2).toSet)
- //转List to String
- println(List(2, 5, 5, 3, 2).toString)
- //Java 与 Scala 集合互转
- //导入包
- import collection.JavaConverters._
- import collection.JavaConversions._
- import java.util.ArrayList;
- // scala 转 java
- val ja =List(1,5,3).asJava
- println(List(1,5,3))
- println(ja)
- // java 转 scala
- val s =new java.util.ArrayList(3).asScala;
- //集合的模式匹配
- val statuses = List(500, 404)
- val msg = statuses.head match {
- case x if x < 500 => "okay"
- case _ => "whoah, an error"
- }
- println("信息:",msg)
- val msg2 = statuses match {
- case x if x contains(500) => "has error"
- case _ => "okay"
- }
- println("信息:",msg2)
- val msg3 = statuses match {
- case List(404, 500) => "not found & error"
- case List(500, 404) => "error & not found"
- case List(200, 200) => "okay"
- case _ => "not sure what happened"
- }
- println("信息:",msg3)
- val msg4 = statuses match {
- case List(500, x) => s"Error followed by $x"
- case List(e, x) => s"$e was followed by $x"
- }
- println("信息:",msg4)
- val head = List(‘r‘,‘g‘,‘b‘) match {
- case x :: xs => x
- case Nil => ‘ ‘
- }
- println(head)
- val code = (‘h‘, 204, true) match {
- case (_, _, false) => 501
- case (‘c‘, _, true) => 302
- case (‘h‘, x, true) => x
- case (c, x, true) => {
- println(s"Did not expect code $c")
- x
- }
- }
- println(code)
- }
顺序输出结果如下:
Java代码
- (List(3, 5, 4), ,11)
- 红色
- false
- (red,红色)
- (green,绿色)
- List(3, 2, 4, 5)
- List(33, 64)
- List(1, 2, 3, 4)
- (List(1, 2),List(3, 4, 5))
- List(3, 2, 1)
- List(3, 5, 7)
- List(a, to, ab, apple)
- List(apple, bag, bbc, one, to)
- (List(2, 3, 5),List(7, 99, 45))
- List(2, 3)
- List((1,a), (2,b))
- List(b, c, d)
- true
- List(1, 2, 3, 4)
- List(1, 2, 2, 3)
- List(1, 2)
- List(1, 6, 5)
- List(ok)
- List(milk, tea)
- 59
- 4.23
- 300
- 42.0
- true
- true
- true
- true
- =======================================
- true
- 23
- 25
- 15
- 15
- 15
- 15
- List(0, 4, 9, 15)
- List(0, 4, 9, 15)
- List(15, 11, 6, 0)
- 24, 99, 104
- ArrayBuffer(f, t)
- List((a,1), (b,2))
- Map(1 -> true, 3 -> true)
- Set(2, 5, 3)
- List(2, 5, 5, 3, 2)
- List(1, 5, 3)
- [1, 5, 3]
- (信息:,whoah, an error)
- (信息:,has error)
- (信息:,error & not found)
- (信息:,Error followed by 404)
- r
- 204
- Process finished with exit code 0
时间: 2024-11-05 20:49:01