Array, tuple, list of collections in Scala

For collections in Scala:

Set A, element b, set B

  1. 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.
  2. Ab, same as above, represents deleting elements
  3. A+=b, which means adding elements to the set A itself
  4. A-=b, which means delete elements from A itself
  5. 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.
  6. A-B, get a new set, the new set has the elements of A, and the elements in B must be removed.
  7. A++=B, append all elements of B to A.
  8. 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...)

Insert picture description here
Insert picture description here

  • 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:

  1. Get elements (access using parentheses (index value))
  2. Add element (+=)
  3. Append a list (++=)
  4. Change the element (use parentheses to get the element and then assign it)
  5. Delete element (-=)
  6. Convert to List (toList)
  7. 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
  1. Determine whether the list is empty (isEmpty)
  2. Concatenate two lists (++)
  3. Get the first element (head) and remaining part (tail) of the list
  4. Reverse list (reverse)
  5. Get the prefix (take), get the suffix (drop)
  6. Flaten
  7. Zipper (zip) and unzip (unzip)
  8. Conversion string (toString)
  9. Generate string (mkString)
  10. Union
  11. Intersection (intersect)
  12. 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.
Insert picture description here

例子:
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)

Guess you like

Origin blog.csdn.net/m0_49834705/article/details/112678969