For collections in Scala:
Set A, element b, set B
- A+b means that a new set is generated, elements are added to the new set, and the content of the original set A remains unchanged.
- Ab, same as above, represents deleting elements
- A+=b, which means adding elements to the set A itself
- A-=b, which means delete elements from A itself
- A++B, get a new set, the new set contains all elements of A and all elements of B. The content of the original collection A has not changed.
- A-B, get a new set, the new set has the elements of A, and the elements in B must be removed.
- A++=B, append all elements of B to A.
- A–=B, remove the elements in B from A.
1. Array
- Fixed-length array
1-长度不可以改变
2-元素可以改变
语法
// 通过指定长度定义数组
val/var 变量名 = new Array[元素类型](数组长度)
// 用元素直接初始化数组
val/var 变量名 = Array(元素1, 元素2, 元素3...)
- Variable length array
语法
1) 创建空的ArrayBuffer变长数组,语法结构:
val/var a = ArrayBuffer[元素类型]()
2) 创建带有初始元素的ArrayBuffer
val/var a = ArrayBuffer(元素1,元素2,元素3....)
Use += to add elements
Use -= to delete elements
Use ++= to append an array to a variable length array
案例:
// 定义变长数组
scala> val a = ArrayBuffer("hadoop", "spark", "flink")
a: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(hadoop, spark, flink)
// 追加一个元素
scala> a += "flume"
res10: a.type = ArrayBuffer(hadoop, spark, flink, flume)
// 删除一个元素
scala> a -= "hadoop"
res11: a.type = ArrayBuffer(spark, flink, flume)
// 追加一个数组
scala> a ++= Array("hive", "sqoop")
res12: a.type = ArrayBuffer(spark, flink, flume, hive, sqoop)
- Traverse the array
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> for(i <- 0 to a.length - 1) println(a(i))
1
2
3
4
5
scala> for(i <- 0 until a.length) println(a(i))
1
2
3
4
5
- Array common algorithm
1) 最大值
2)最小值
3)求和
4)排序
scala> val a = ArrayBuffer(1,2,3,4,18000)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 18000)
scala> a.min
res1: Int = 1
scala> a.sum
res2: Int = 18010
scala> a.sorted
res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 18000)
scala> a.max
res4: Int = 18000
scala>
2. Tuples
- Tuples can be used to contain a set of values of different types. For example: name, age, gender, date of birth. The elements of a tuple are immutable .
语法
使用括号来定义元组
val/var 元组 = (元素1, 元素2, 元素3....)
使用箭头来定义元组(只支持元组只有两个元素时)
val/var 元组 = 元素1->元素2
定义:
1)
scala> val a = ("liuafu","kobe","James",2021)
a: (String, String, String, Int) = (liuafu,kobe,James,2021)
2)
scala> val a = ("kobe"->41)
a: (String, Int) = (kobe,41)
访问:
1)
访问第一个元素:
scala> a._1
res6: String = kobe
访问第二个元素:
scala> a._2
res7: Int = 41
3. List
- List is the most important and commonly used data structure in scala. It is equivalent to list in Java
- Repeat orderly
Immutable list:
定义:列表的元素、长度都是不可变的。
语法:
1) 使用List(元素1, 元素2, 元素3, ...)来创建一个不可变列表,语法格式:
val/var 变量名 = List(元素1, 元素2, 元素3...) //最常用
2) 使用Nil创建一个不可变的空列表
val/var 变量名 = Nil
3) 使用::方法创建一个不可变列表
val/var 变量名 = 元素1 :: 元素2 :: Nil
例如:
scala> val a = List(18000,20000,22000)
a: List[Int] = List(18000, 20000, 22000)
Variable list:
定义: 列表的元素、长度都是可变的。
语法:
使用ListBuffer[元素类型]()创建空的可变列表,语法结构:
val/var 变量名 = ListBuffer[T]()
使用ListBuffer(元素1, 元素2, 元素3...)创建可变列表,语法结构:
val/var 变量名 = ListBuffer(元素1,元素2,元素3...)
Operation variable list:
- Get elements (access using parentheses (index value))
- Add element (+=)
- Append a list (++=)
- Change the element (use parentheses to get the element and then assign it)
- Delete element (-=)
- Convert to List (toList)
- Convert to Array (toArray)
//创建一个可变列表
scala> val a = ListBuffer(1,2,3)
a: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
//获取列表的第一个元素
scala> a(0)
res8: Int = 1
//添加元素
scala> a +=4
res9: a.type = ListBuffer(1, 2, 3, 4)
//添加一个列表
scala> a ++=List(18000,20000,22000)
res10: a.type = ListBuffer(1, 2, 3, 4, 18000, 20000, 22000)
//删除一个元素
scala> a -=1
res11: a.type = ListBuffer(2, 3, 4, 18000, 20000, 22000)
//转为一个数组
scala> a.toArray
res12: Array[Int] = Array(2, 3, 4, 18000, 20000, 22000)
//转为一个不可变的列表
scala> a.toList
res13: List[Int] = List(2, 3, 4, 18000, 20000, 22000)
- Common operations for lists
- Determine whether the list is empty (isEmpty)
- Concatenate two lists (++)
- Get the first element (head) and remaining part (tail) of the list
- Reverse list (reverse)
- Get the prefix (take), get the suffix (drop)
- Flaten
- Zipper (zip) and unzip (unzip)
- Conversion string (toString)
- Generate string (mkString)
- Union
- Intersection (intersect)
- Difference (diff)
scala> val a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)
//判断是否为空
scala> a.isEmpty
res14: Boolean = false
//拼接两个列表
scala> val b = List(18000,20000,22000)
b: List[Int] = List(18000, 20000, 22000)
scala> a ++ b
res16: List[Int] = List(1, 2, 3, 4, 18000, 20000, 22000)
//反转
scala> b.reverse
res17: List[Int] = List(22000, 20000, 18000)
//拉链
scala> val a = List("zhangsan", "lisi", "wangwu")
a: List[String] = List(zhangsan, lisi, wangwu)
scala> val b = List(19, 20, 21)
b: List[Int] = List(19, 20, 21)
scala> a.zip(b)
res1: List[(String, Int)] = List((zhangsan,19), (lisi,20), (wangwu,21))
//拉开
scala> res1.unzip
res2: (List[String], List[Int]) = (List(zhangsan, lisi, wangwu),List(19, 20, 21))
// Flattening: Flattening means putting all the elements in the list into one list.
例子:
scala> val a =List(List(12,15),List(25,30),List(55,20))
a: List[List[Int]] = List(List(12, 15), List(25, 30), List(55, 20))
scala> a.flatten
res18: List[Int] = List(12, 15, 25, 30, 55, 20)