Scala集合,序列 可变和不可变List List各种函数的使用 不可变Set和可变Set Map
程序员文章站
2022-05-13 23:51:29
...
1. 集合
Scala的集合有三大类:序列Seq、集Set、映射Map,所有的集合都扩展自Iterable特质
在Scala中集合有可变(mutable)和不可变(immutable)两种类型,immutable类型的集合初始化后就不能改变了(注意与val修饰的变量进行区别)
1.1. 序列
不可变的序列 import scala.collection.immutable._
在Scala中列表要么为空(Nil表示空列表)要么是一个head元素加上一个tail列表。
9 :: List(5, 2) :: 操作符是将给定的头和尾创建一个新的列表
注意::: 操作符是右结合的,如9 :: 5 :: 2 :: Nil相当于 9 :: (5 :: (2 :: Nil))
package cn.toto.scala/** * Created by toto on 2017/6/28. */object ImmutListDemo { def main(args: Array[String]): Unit = { //创建一个不可变的集合 val lst1 = List(1,2,3) println(lst1) //将0插入到lst1的前面生成一个新的List val lst2 = 0::lst1 println(lst2) val lst3 = lst1.::(0) println(lst3) val lst4 = 0 +: lst1; println(lst4) val lst5 = lst1.+:(0) println(lst5) //将一个元素添加到list1的后面产生一个新的集合 val lst6 = lst1 :+3 val lst0 = List(4,5,6) //将2个list合并成一个新的List val lst7 = lst1 ++ lst0 println(lst7) //将lst0插入到lst1前面生成一个新的集合 val lst9 = lst1.:::(lst0) println(lst9) }}
- 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
运行结果:
List(1, 2, 3)List(0, 1, 2, 3)List(0, 1, 2, 3)List(0, 1, 2, 3)List(0, 1, 2, 3)List(1, 2, 3, 4, 5, 6)List(4, 5, 6, 1, 2, 3)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
可变的序列 import scala.collection.mutable._
package cn.toto.scalaimport scala.collection.mutable.ListBuffer/** * Created by toto on 2017/6/28. */object MuListDemo { def main(args: Array[String]): Unit = { //构建一个可变列表,初始有3个元素,1,2,3 val lst0 = ListBuffer[Int](1,2,3) println(lst0) //创建一个空的可变列表 val lst1 = new ListBuffer[Int] //向lst1中追加元素,注意:没有生成新的集合 lst1 += 4 println(lst1) lst1.append(5) println(lst1) //将lst1中的元素最近到lst0中,注意:没有生成新的集合 lst0 ++= lst1 println(lst0) //将lst0 和 lst1合并成一个新的ListBuffer,注意:生成一个集合 val lst2 = lst0 ++ lst1 println(lst2) //将元素追加到lst0的后面生成一个新的集合 val lst3 = lst0 :+ 5 println(lst3) }}
- 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
运行后的结果如下:
ListBuffer(1, 2, 3)ListBuffer(4)ListBuffer(4, 5)ListBuffer(1, 2, 3, 4, 5)ListBuffer(1, 2, 3, 4, 5, 4, 5)ListBuffer(1, 2, 3, 4, 5, 5)
- 1
- 2
- 3
- 4
- 5
- 6
其它案例:
package cn.toto.scala/** * Created by toto on 2017/6/28. */object ListTest { def main(args: Array[String]): Unit = { //创建一个List val lst0 = List(1,7,9,8,0,3,5,4,6,2) //将lst0中每个元素乘以10后生成一个新的集合 val lst1 = lst0.map(x => x * 2) //结果是:List(2, 14, 18, 16, 0, 6, 10, 8, 12, 4) println(lst1) //将lst0中的偶数取出来生成一个新的集合 val lst2 = lst0.filter(x => x % 2 == 0) //运行结果是:List(8, 0, 4, 6, 2) println(lst2) //将lst0排序后生成一个新的集合 val lst3 = lst0.sorted //运行结果:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) println(lst3) val lst4 = lst0.sortBy(x => x) //运行结果:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) println(lst4) val lst5 = lst0.sortWith((x,y) => x < y) //运行结果:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) println(lst5) //反转顺序 val lst6 = lst3.reverse //运行结果:List(9, 8, 7, 6, 5, 4, 3, 2, 1, 0) println(lst6) //将Iterator转换成List val lst7 = lst0.toList //运行结果是:List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2) println(lst7) //先按空格切分,再压平 val a = Array("a b c","d e f","h i j") a.flatMap(_.split(" ")) //运行结果是:[Ljava.lang.String;@54a097cc println(a) val value1 = lst0.reduce(_+_) //运行结果是:45 println(value1) val value2 = lst0.fold(10)(_+_) //运行结果是:55 println(value2) //并行计算求和 val value3 = lst0.par.sum //运行结果是:45 println(value3) val value4 = lst0.par.map(_ % 2 == 0) //运行结果:ParVector(false, false, false, true, true, false, false, true, true, true) println(value4) val value5 = lst0.par.reduce((x,y) => x + y) //运行结果:45 println(value5) //简化:reduce //将非特定顺序的二元操作应用到所有元素 val lst9 = lst0.par.reduce((x, y) => x + y) //运行结果是:45 println(lst9) //按照特定的顺序 val lst10 = lst0.reduceLeft(_+_) //运行结果:45 println(lst10) //折叠:有初始值(无特定顺序) val lst11 = lst0.par.fold(100)((x,y) => x + y) //运行结果是:945,第二次是1045,最后又回到945 println(lst11) //折叠:有初始值(有特定顺序) val lst12 = lst0.foldLeft(100)((x, y) => x + y) //运行结果一直是145 println(lst12) //聚合 val arr = List(List(1, 2, 3), List(3, 4, 5), List(2), List(0)) val result = arr.aggregate(0)(_+_.sum,_+_) //运行结果:20 println(result) val l1 = List(5,6,4,7) val l2 = List(1,2,3,4) //求并集 val r1 = l1.union(l2) //结果是:List(5, 6, 4, 7, 1, 2, 3, 4) println(r1) //求交集 val r2 = l1.intersect(l2) //运行结果:List(4)。并集只有一个4 println(r2) //求差集 val r3 = l1.diff(l2) //求l1中不包含l2元素的集合,运行结果是:List(5, 6, 7) println(r3) }}
- 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
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
2. Set
不可变的Set
package cn.toto.collectimport scala.collection.immutable.HashSetobject ImmutSetDemo extends App{ val set1 = new HashSet[Int]() //将元素和set1合并生成一个新的set,原有set不变 val set2 = set1 + 4 //set中元素不能重复 val set3 = set1 ++ Set(5, 6, 7) val set0 = Set(1,3,4) ++ set1 println(set0.getClass)}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
可变的Set
package cn.toto.scalaimport scala.collection.mutable/** * Created by toto on 2017/6/28. */object MutSetDemo { def main(args: Array[String]): Unit = { //创建一个可变的HashSet val set1 = new mutable.HashSet[Int](); //向HashSet中添加元素 set1 += 2 //add等价于+= set1.add(4) println(set1) set1 ++= Set(1,3,5) println(set1) //删除一个元素 set1 -= 5 println(set1) set1.remove(2) println(set1) }}
- 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
运行结果:
Set(2, 4)Set(1, 5, 2, 3, 4)Set(1, 2, 3, 4)Set(1, 3, 4)
- 1
- 2
- 3
- 4
3. Map
package cn.toto.scalaimport scala.collection.mutable/** * Created by toto on 2017/6/28. */object MutMapDemo { def main(args: Array[String]): Unit = { val map1 = new mutable.HashMap[String,Int](); //向map中添加数据 map1("spark") = 1 map1 += (("hadoop",2)) map1.put("storm",3) println(map1) //从map中移除元素 map1 -= "spark" map1.remove("hadoop") println(map1) }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
运行后的结果如下:
Map(hadoop -> 2, spark -> 1, storm -> 3)Map(storm -> 3)
- 1
- 2
其它零碎的内容:
下面的代码是在命令提示符上输入的内容:
scala> def m(x:Int,y:Int):Int={ x + y }m: (x: Int, y: Int)Intscala> m(1,2)res0: Int = 3scala> (x:Int,y:Int) => x + yres1: (Int, Int) => Int = $$Lambda$1044/698062929@191a0351scala> val f = (x:Int,y:Int) => x + yf: (Int, Int) => Int = $$Lambda$1045/1726169577@5b48f0f4scala> val f1 :(Int,Int) => Int = {(x,y) => x + y}f1: (Int, Int) => Int = $$Lambda$1046/535361000@39449465scala> f1(1,2)res2: Int = 3scala> Array(1,2,3,4,5,6,7,8,9,10)res3: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> val arr = Array(1,2,3,4,5,6,7,8,9,10)arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> arr.map(_ * 10)res4: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> arrres6: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> arr.map((x:Int) => x * 10)res7: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> arr.map(x => x * 10)res8: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> val f2 = (x:Int) => x * 10f2: Int => Int = $$Lambda$1172/1943764464@44a485bcscala> arr.map(f2)res9: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> def m1(x:Int):Int = x * 10m1: (x: Int)Intscala> arr.map(m1)res10: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> arr.sortBy(x => x)res11: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)#成了按照字符串进行排序了。scala> arr.sortBy(x => x + "")res12: Array[Int] = Array(1, 10, 2, 3, 4, 5, 6, 7, 8, 9)scala> arr.fold(0)(_+_)res13: Int = 55scala> arr.reduce(_+_)res14: Int = 55scala> arr.fold(10)(_+_)res15: Int = 65scala> arr.parres16: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)#这里相当于是使用多个线程进行处理的scala> arr.par.sumres17: Int = 55聚合scala> val arr = List(List(1,2,3),List(3,4,5),List(2),List(0))arr: List[List[Int]] = List(List(1, 2, 3), List(3, 4, 5), List(2), List(0))这里的a代表的是0,b代表的是上面的List中的每个list,(x,y) => x + y表示给b.sum中的结果进行求和scala> arr.aggregate(0)((a,b)=> a + b.sum,(x,y) => x + y)res0: Int = 20scala>
- 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
- 78
- 79
- 80
- 81
- 82
- 83
上一篇: 【java基础】HashSet源码分析
下一篇: 集合list和set排序的实现