前言:
数组和集合都分为可变,不可变,下面都会说到哦
数组,集合,元组
数组Array与ArrayBuffer
ArrayBuffer与Array的区别
- Array:数组长度不可变,定长数组
- ArrayBuffer:数组长度可变,变长数组
Array的定义,赋值和取值
定义
//用于指定数组长度,没有指定初始值,必须加new
scala> val a1 = new Array[Int](5)
a1: Array[Int] = Array(0, 0, 0, 0, 0)
//指定了初始值可以不加new
scala> val a2 = Array(1,2,3,4,5)
a2: Array[Int] = Array(1, 2, 3, 4, 5)
赋值
scala> a1(0) = 10
scala> a1
res1: Array[Int] = Array(10, 0, 0, 0, 0)
- Scala中引用下标使用圆括号
- 下标从0开始
取值
scala> a1(0)
res2: Int = 10
scala> println(a1(0))
10
scala> for(i <- a1 ) println(i)
10
0
0
0
0
#后面文章会说到更好取数组中的元素(foreach)
ArrayBuffer的定义、赋值及取值
定义
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val ab1 = new ArrayBuffer[Int]()
ab1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> val ab2 = ArrayBuffer(1,2,3)
ab2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
增加:+=,append
scala> ab1 += 10
res13: ab1.type = ArrayBuffer(10)
scala> ab1 += 9
res14: ab1.type = ArrayBuffer(10, 9)
scala> ab1 += 7
res15: ab1.type = ArrayBuffer(10, 9, 7)
scala> ab1.append(8)
scala> ab1
res17: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 9, 7, 8)
scala> a1
res18: Array[Int] = Array(10, 0, 0, 0, 0)
赋值
scala> ab1(1) = 8
scala> ab1
res21: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 8, 7, 8)
取值
scala> for(x <- ab1) println(x)
10
8
7
8
删除
//按照值进行剔除
scala> ab1 -= 8
res23: ab1.type = ArrayBuffer(10, 7, 8)
//按照下标剔除
scala> ab1.remove(1)
res24: Int = 7
scala> ab1
res25: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 8)
数组中常用的操作
排序:sorted
scala> ab2.sorted
res31: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 3, 5, 7, 10)
反转:reverse
scala> ab2.sorted.reverse
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 7, 5, 3, 3, 2, 1)
求和:sum
scala> ab2.sum
res33: Int = 31
最大值:max
scala> ab2.max
res34: Int = 10
最小值:min
scala> ab2.min
res35: Int = 1
转换为字符串:mkString
scala> ab2.mkString
res38: String = 12373510
scala> ab2.mkString("-")
res39: String = 1-2-3-7-3-5-10
scala> ab2.mkString(",")
res40: String = 1,2,3,7,3,5,10
scala> ab2.mkString("<",",",">")
res41: String = <1,2,3,7,3,5,10>
scala> ab2.mkString("<","-",">")
res42: String = <1-2-3-7-3-5-10>
列表List与ListBuffer
List与ListBuffer的区别
- List:不可变的集合,长度和元素值都不可变
- ListBuffer:可变的集合,长度和元素值都可变
List的定义、赋值及取值
定义
scala> val list1 = List(1,2,3,4,5,6,6,1,2)
list1: List[Int] = List(1, 2, 3, 4, 5, 6, 6, 1, 2)
scala> list1(0) = 10
<console>:14: error: value update is not a member of List[Int]
list1(0) = 10
^
注意:值不可变
取值
scala> list1(0)
res44: Int = 1
scala> for(i <- list1) println(i)
1
2
3
4
5
6
6
1
2
ListBuffer的定义、赋值及取值
定义
scala> val listb1 = new ListBuffer[Int]()
listb1: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> val listb2 = ListBuffer(1,2,3,4,3,2,1)
listb2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 3, 2, 1)
添加:+=,append
scala> listb1 += 1
res52: listb1.type = ListBuffer(10, 9, 8, 1)
scala> listb1.append(3)
scala> listb1
res57: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 2, 8, 1, 3)
赋值
scala> listb1(1) = 2
scala> listb1
res55: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 2, 8, 1)
删除:-=,remove
scala> listb1 -= 10
res58: listb1.type = ListBuffer(2, 8, 1, 3)
scala> listb1.remove(0)
res59: Int = 2
scala> listb1
res60: scala.collection.mutable.ListBuffer[Int] = ListBuffer(8, 1, 3)
取值
scala> listb1(0)
res61: Int = 8
scala> for(x <- listb1) println(x)
8
1
3
List于ListBuffer的常用操作
list常用操作
取第1个元素:head
取除了第一个元素以外的元素:tail
取前N个元素:take
取除了前N个元素以外的所有元素:drop
定义一个空list集合:Nil
集合头部添加元素返回新的集合:+:、::
集合尾部添加元素返回新的集合::+
拼接两个集合的元素返回新的集合:++、:::
扁平化操作:将两层集合转换为一层集合:flatten
拉链操作:zip、zipWithIndex
拉开操作:unzip
并集:union
交集:intersect
差集:diff
ListBuffer集合常用操作
转换为List:toList
转换为Array:toArray
详细用法和其他方法链接: https://blog.csdn.net/weixin_44147632/article/details/108513800
Set集合
Set集合的特点
- 无序不重复集合,一般用于实现数据的去重功能
可变Set集合与不可变Set的区别
- immutable.Set:不可变Set集合,长度不可变
- mutable.Set:可变Set集合,长度可变
不可变Set集合的定义与读取
定义
scala> val set1 = Set(1,2,3,4,4,2,1,3)
set1: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
取值
scala> for(i <- set1 ) println(i)
1
2
3
4
可变Set集合的定义、赋值与读取
定义
scala> import scala.collection.mutable.Set
import scala.collection.mutable.Set
scala> val set2 = Set(1,2,3,4,4,2,1,3)
set2: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
添加:+=
scala> set2 += 4
res102: set2.type = Set(1, 5, 2, 3, 4)
删除:-=
scala> set2 -= 2
res103: set2.type = Set(1, 5, 3, 4)
读取
scala> for(i <- set2) println(i)
1
5
3
4
Map集合
Map集合的特点
- 存储KeyValue格式数据的集合
- 作为大数据工程师:处理最多的数据结构类型:KV结构类型
可变Map与不可变Map的区别
- immutable.Map:不可变Map,不可添加、删除、更新元素
- mutable.Map:可变Map,可以添加、删除、更新元素
不可变Map集合的定义、读取
定义
scala> val map1 = Map("name" -> "laoda","age" -> "20","gender"->"male")
map1: scala.collection.immutable.Map[String,String] = Map(name -> laoda, age -> 20, gender -> male)
scala> val map2 = Map(("name","laoer"),("age","22"),("gender","female"))
map2: scala.collection.immutable.Map[String,String] = Map(name -> laoer, age -> 22, gender -> female)
读取
-
方式一:直接根据Key读取
scala> map1("name") res105: String = laoda scala> map1("name1") java.util.NoSuchElementException: key not found: name1 at scala.collection.MapLike$class.default(MapLike.scala:228) at scala.collection.AbstractMap.default(Map.scala:59) at scala.collection.MapLike$class.apply(MapLike.scala:141) at scala.collection.AbstractMap.apply(Map.scala:59) ... 32 elided
一般不用,如果key不存在,会报错
-
方式二:使用get方法
scala> map1.get def get(key: String): Option[String] scala> map1.get("name") res107: Option[String] = Some(laoda) scala> map1.get("name").get res108: String = laoda scala> map1.get("name1") res109: Option[String] = None
-
方式三:使用getOrElse方法
scala> map1.getOrElse("name1","null") res112: String = null scala> map1.getOrElse("name","null") res113: String = laoda
-
方式四:循环挨个取值
scala> for(x <- map1) println(x) (name,laoda) (age,20) (gender,male) scala> for((key,value) <- map1) println(key+"\t"+value) name laoda age 20 gender male
可变Map集合的定义、添加、读取
定义
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val map3 = Map(("name","laoer"),("age","22"),("gender","female"))
map3: scala.collection.mutable.Map[String,String] = Map(age -> 22, name -> laoer, gender -> female)
添加:+=
scala> map3 += "addr" -> "shanghai"
res116: map3.type = Map(addr -> shanghai, age -> 22, name -> laoer, gender -> female)
更新:=
scala> map3("addr") = "beijing"
scala> map3
res120: scala.collection.mutable.Map[String,String] = Map(addr -> beijing, age -> 22, gender -> female)
删除:remove
scala> map3.remove("name")
res117: Option[String] = Some(laoer)
scala> map3
res118: scala.collection.mutable.Map[String,String] = Map(addr -> shanghai, age -> 22, gender -> female)
获取Key:keys
scala> map3.keys
res121: Iterable[String] = Set(addr, age, gender)
获取Value:values
scala> map3.values
res122: Iterable[String] = HashMap(beijing, 22, female)
遍历
for(x <- map3) println(x)
for((key,value)<- map3) println(key+"\t"+value)
for(key <- map3.keys) println(map3(key))
元组Tuple
元组的功能和特点
- 功能:类似于数组,用于存储不同类型的元素的集合
- 区别
- 数组:数组中只能存储一种类型的元素
- 元组:元组中可以存储不同类型的元素
元组的语法
val/var tupleName = (元素1,元素2,元素3,元素4……)
val/var tupleName = TupleN(元素1,元素2,元素3,元素4……元素N)
举例
scala> val tuple1 = (1,2,"itcast",14.9,true)
tuple1: (Int, Int, String, Double, Boolean) = (1,2,itcast,14.9,true)
scala> val tuple2 = new Tuple3(1,2,"heima")
tuple2: (Int, Int, String) = (1,2,heima)
- 特殊:二元组就是KeyValue对
- 方法:swap:二元组特有的方法
- 将key和value的位置互换
- 元组最多只能有22个元素
元组的取值
scala> tuple1._1
res126: Int = 1
scala> tuple1._2
res127: Int = 2
scala> tuple1._3
res128: String = itcast
迭代器Iterator
-
scala针对每一类集合都提供了一个迭代器(iterator)用来迭代访问集合
-
迭代器的两个基本方法
- hasNext:查询容器中是否有下一个元素
- next:返回迭代器的下一个元素,如果没有,抛出NoSuchElementException
举例
scala> val array = Array(1,2,3,4,5)
array: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val itera = array.iterator
itera: Iterator[Int] = non-empty iterator
scala> while(itera.hasNext) println(itera.next)
1
2
3
4
5
迭代器基本与java是一致的
通过hasNext判断,通过next方法取值
常用方法:toList等,用于实现迭代器的排序操作