1. 程式人生 > >scala開發快速入門 | 第四篇 資料結構

scala開發快速入門 | 第四篇 資料結構

一、集(Set)

Scala中的集合分為可變的集合、不可變的集合。Set中的資料是無序的,不重複的。

預設情況下,Scala使用的是不可變的集合,預設引用的是 scala.collection.immutable.Set。

                    如果你想使用可變的集合,需要引用 scala.collection.mutable.Set 包。

區別:

1)可變集合可以修改,新增,移除一個集合的元素;原集合會發生變化。

2)而不可變集合類,相比之下,永遠不會改變。仍然可以模擬新增,移除或更新操作。但是這些操作將在每一種情況下都返回一個新的集合,同時使原來的集合不發生改變。

object Operator_Set {
  def 
main(args: Array[String]): Unit = { /*不可變集合操作*/ val set=Set(1,2,3); println(set.getClass.getName)//scala.collection.immutable.Set$Set3 預設是不可變的集合 println(set.exists(_ % 2 == 0)) //true println(set.drop(1)) //Set(2,3) println(set) //Set(1,2,3) /*可變集合*/ import scala.collection.mutable.Set // 可以在任何地方引入 可變集合 val
mutableSet = Set(1,2,3) println(mutableSet.getClass.getName) // scala.collection.mutable.HashSet mutableSet.add(4) println(mutableSet) // Set(1, 2, 3, 4) mutableSet.remove(1) println(mutableSet) // Set(2, 3, 4) } }

集合基本操作

Scala集合有三個基本操作:

  • head 返回集合第一個元素
  • tail 返回一個集合,包含除了第一元素之外的其他元素
  • isEmpty 在集合為空時返回true
val setOperator=Set("google","baidu","netcloud")
val setContainsInt:Set[Int]=Set()
println(setOperator.head)//google
println(setOperator.tail)//Set(netcloud, baidu)
println(setOperator.isEmpty)//false
println(setContainsInt.isEmpty)//true

集合的連線

可以使用++運算子和Set中的 .++() 方法來連線兩個集合,如果兩個集合中存在重複的元素就會移除。

val site1 = Set("Runoob", "Google", "Baidu","kugou")

val site2 = Set("Faceboook", "Taobao","kugou")
// ++ 作為運算子使用
var site = site1 ++ site2
println( "site1 ++ site2 : " + site )
//  ++ 作為方法使用
site = site1.++(site2)
println( "site1.++(site2) : " + site )
site1 ++ site2 : Set(Google, Faceboook, Runoob, Baidu, Taobao, kugou)
site1.++(site2) : Set(Google, Faceboook, Runoob, Baidu, Taobao, kugou)
查詢集合中最大最小值

val num=Set(1,2,3,4,5)
println(num.max)//5
println(num.min)//1

交集

可以使用 Set.& 方法或 Set.intersect 方法來檢視兩個集合的交集元素

val num1=Set(1,2,3,4,5)
val num2=Set(4,5,6,7,8)
println(num1.&(num2))
// 交集
println( "num1.&(num2) : " + num1.&(num2) ) //num1.&(num2) : Set(5, 4)
println( "num1.intersect(num2) : " + num1.intersect(num2) ) //num1.intersect(num2) : Set(5, 4)

遍歷集合

val iterator=Set("Spark","Scala","Hive","Impala")
for(i <- iterator){
  println(i)
}

Scala集合Set中常用的方法

下表列出了 Scala Set 常用的方法:

序號方法及描述
1

def +(elem: A): Set[A]

為集合新增新元素,x並建立一個新的集合,除非元素已存在

2

def -(elem: A): Set[A]

移除集合中的元素,並建立一個新的集合

3

def contains(elem: A): Boolean

如果元素在集合中存在,返回 true,否則返回 false。

4

def &(that: Set[A]): Set[A]

返回兩個集合的交集

5

def &~(that: Set[A]): Set[A]

返回兩個集合的差集

6

def +(elem1: A, elem2: A, elems: A*): Set[A]

通過新增傳入指定集合的元素建立一個新的不可變集合

7

def ++(elems: A): Set[A]

合併兩個集合

8

def -(elem1: A, elem2: A, elems: A*): Set[A]

通過移除傳入指定集合的元素建立一個新的不可變集合

9

def addString(b: StringBuilder): StringBuilder

將不可變集合的所有元素新增到字串緩衝區

10

def addString(b: StringBuilder, sep: String): StringBuilder

將不可變集合的所有元素新增到字串緩衝區,並使用指定的分隔符

11

def apply(elem: A)

檢測集合中是否包含指定元素

12

def count(p: (A) => Boolean): Int

計算滿足指定條件的集合元素個數

13

def copyToArray(xs: Array[A], start: Int, len: Int): Unit

複製不可變集合元素到陣列

14

def diff(that: Set[A]): Set[A]

比較兩個集合的差集

15

def drop(n: Int): Set[A]]

返回丟棄前n個元素新集合

16

def dropRight(n: Int): Set[A]

返回丟棄最後n個元素新集合

17

def dropWhile(p: (A) => Boolean): Set[A]

從左向右丟棄元素,直到條件p不成立

18

def equals(that: Any): Boolean

equals 方法可用於任意序列。用於比較系列是否相等。

19

def exists(p: (A) => Boolean): Boolean

判斷不可變集合中指定條件的元素是否存在。

20

def filter(p: (A) => Boolean): Set[A]

輸出符合指定條件的所有不可變集合元素。

21

def find(p: (A) => Boolean): Option[A]

查詢不可變集合中滿足指定條件的第一個元素

22

def forall(p: (A) => Boolean): Boolean

查詢不可變集合中滿足指定條件的所有元素

23

def foreach(f: (A) => Unit): Unit

將函式應用到不可變集合的所有元素

24

def head: A

獲取不可變集合的第一個元素

25

def init: Set[A]

返回所有元素,除了最後一個

26

def intersect(that: Set[A]): Set[A]

計算兩個集合的交集

27

def isEmpty: Boolean

判斷集合是否為空

28

def iterator: Iterator[A]

建立一個新的迭代器來迭代元素

29

def last: A

返回最後一個元素

30

def map[B](f: (A) => B): immutable.Set[B]

通過給定的方法將所有元素重新計算

31

def max: A

查詢最大元素

32

def min: A

查詢最小元素

33

def mkString: String

集合所有元素作為字串顯示

34

def mkString(sep: String): String

使用分隔符將集合所有元素作為字串顯示

35

def product: A

返回不可變集合中數字元素的積。

36

def size: Int

返回不可變集合元素的數量

37

def splitAt(n: Int): (Set[A], Set[A])

把不可變集合拆分為兩個容器,第一個由前 n 個元素組成,第二個由剩下的元素組成

38

def subsetOf(that: Set[A]): Boolean

如果集合中含有子集返回 true,否則返回false

39

def sum: A

返回不可變集合中所有數字元素之和

40

def tail: Set[A]

返回一個不可變集合中除了第一元素之外的其他元素

41

def take(n: Int): Set[A]

返回前 n 個元素

42

def takeRight(n: Int):Set[A]

返回後 n 個元素

43

def toArray: Array[A]

將集合轉換為陣列

44

def toBuffer[B >: A]: Buffer[B]

返回緩衝區,包含了不可變集合的所有元素

45

def toList: List[A]

返回 List,包含了不可變集合的所有元素

46

def toMap[T, U]: Map[T, U]

返回 Map,包含了不可變集合的所有元素

47

def toSeq: Seq[A]

返回 Seq,包含了不可變集合的所有元素

48

def toString(): String

返回一個字串,以物件來表示

二、陣列(Array)

Scala中的陣列分為定長的陣列和變長的陣列;定長的陣列在陣列定義的時候長度需要被確定,在執行的時候陣列的長度不會發生改變,而變長的陣列記憶體儲存長度會隨著程式執行的需要而動態的擴容。

1)定長陣列

定長陣列定義的方式有兩種,一種是new Array[T](length)方式‘另一種是無new操作’ 即:Array(element)

def main(args: Array[String]): Unit = {
  //定義一個長度10的數值陣列
val numberArray=new Array[Int](10)//numberArray: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  //定義一個長度為10的String型別的陣列
val stringArray=new Array[String](10)//stringArray: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
 /*陣列元素賦值*/
numberArray(0)=666  // res1: Array[Int] = Array(666, 0, 0, 0, 0, 0, 0, 0, 0, 0)
stringArray(1)="Scala" // res3: Array[String] = Array(null, Scala, null, null, null, null, null, null, null, null)
  /*無new操作的方式 定義陣列 需要給陣列進行賦值*/
val stringArray2=Array("Scala","Spark","Hive")

2)變長的陣列 ArrayBuffer

變長陣列在程式的