Scala教程-2 集合操作

本博客簡要講述了Scala集合的核心操作,是學習Scala必須掌握的基礎知識,後續更新Scala的高級語法糖。

2. 集合操作

2.1 List集合

2.1.1 聲明一個List集合

scala> val list = List(1,2,3,4,5,6)
list: List[Int] = List(1, 2, 3, 4, 5, 6)

2.1.2 List集合的map操作

操作一: 基本操作

scala> val newList = list.map((x:Int)=>2*x)
newList: List[Int] = List(2, 4, 6, 8, 10, 12)

操作二: 由於List中只有一種元素類型,所以可以省略參數類型

scala> val newList = list.map((x)=>2*x)
newList: List[Int] = List(2, 4, 6, 8, 10, 12)

操作三: 如果只有一個參數,則可以省略參數列表的括號

scala> val newList = list.map(x=>2*x)
newList: List[Int] = List(2, 4, 6, 8, 10, 12)

操作四: 使用佔位符表示集合中的元素

scala> val newList = list.map(_*2)
newList: List[Int] = List(2, 4, 6, 8, 10, 12)

2.2 Set集合

scala> val ss = Set(1,2,1)
ss: scala.collection.immutable.Set[Int] = Set(1, 2)

注意: Set集合中不存在重複元素,如果有重複元素則會去掉重複的元素

2.3 Tuple集合

2.3.1 創建Tuple集合

操作1: 創建普通Tuple集合

scala> val hostPort=("localhost","8080")
hostPort: (String, String) = (localhost,8080)

scala> hostPort._1
res6: String = localhost

scala> hostPort._2
res7: String = 8080

注意: Tuple集合中可以放置不同類型的元素;

​ Tuple集合中的元素訪問是從下標1開始訪問的;

操作2: 創建2個元素的Tuple集合

scala> "a"->"b"
res8: (String, String) = (a,b)

2.3.2 賦值操作

scala> val tuple = (1,2,3,4,"lubin","spark")
tuple: (Int, Int, Int, Int, String, String) = (1,2,3,4,lubin,spark)

scala> val (first,second,third,fourth,fifth,six)=tuple
first: Int = 1
second: Int = 2
third: Int = 3
fourth: Int = 4
fifth: String = lubin
six: String = spark

注意: 上面tuple的長度必須與接收的元組長度一致,否則報錯

scala> val (f1,f2,_,_,_,_)=tuple
f1: Int = 1
f2: Int = 2

注意: 上述佔位符”_”表示此位置不需要賦值

scala> "Spark Hadoop".partition(_.isUpper)
res10: (String, String) = (SH,park adoop)

注意: partition函數的參數爲一個布爾表達式函數,判斷字符串中的字符是否爲大寫;最後將大寫字符組合爲一個字符串,非大寫字符組合爲一個字符串形成元組

2.4 Map集合

2.4.1 創建Map集合

操作1: 創建immutable類型Map

scala> Map("a"->"b","lubin"->"10000")
res11: scala.collection.immutable.Map[String,String] = Map(a -> b, lubin -> 10000)

注意: 創建Map使用的可變參數方式;

​ 創建的 Map默認是immutable類型

操作2: 創建mutable類型Map

scala> val map = scala.collection.mutable.Map("apple"->7,"spark"->8)
map: scala.collection.mutable.Map[String,Int] = Map(spark -> 8, apple -> 7)

2.5 Option類型

Option類型表示一個值是可選的(有值或無值)

Option[T] 是一個類型爲 T 的可選值的容器: 如果值存在,Option[T] 就是一個 Some[T] ,如果不存在, Option[T] 就是對象 None 。

scala> val map = Map("key1"->"spark","key2"->100)
map: scala.collection.immutable.Map[String,Any] = Map(key1 -> spark, key2 -> 100
)

scala> val value1 = map.get("key1")
value1: Option[Any] = Some(spark)

scala> val value2 = map.get("key2")
value2: Option[Any] = Some(100)

scala> val value3 = map.get("key3")
value3: Option[Any] = None

使用getOrElse() 函數來獲取Option中的值,如果有值,則返回實際值,沒有則返回默認值

scala> value2.getOrElse(0)
res14: Any = 100

scala> value3.getOrElse(0)
res15: Any = 0

2.6 filter操作

scala> val list = List(1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5)

scala> list.filter(x=>x%2==0)
res16: List[Int] = List(2, 4)

注意: filter函數接受一個boolean表達式,作用爲過濾出滿足布爾條件的值組成的集合;順序與原來一致;

2.7 zip操作

scala> val a=List(1,2,3)
a: List[Int] = List(1, 2, 3)

scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6)

scala> a zip b
res17: List[(Int, Int)] = List((1,4), (2,5), (3,6))

注意: a zip b 可以寫成a.zip(b)

scala> val a=List(1,2,3)
a: List[Int] = List(1, 2, 3)

scala> val b = List(1,2,3,4)
b: List[Int] = List(1, 2, 3, 4)

scala> a zip b
res18: List[(Int, Int)] = List((1,1), (2,2), (3,3))
scala> val array = Array("[","-","]")
array: Array[String] = Array([, -, ])

scala> val nums = Array(2,5,6)
nums: Array[Int] = Array(2, 5, 6)

scala> val pairs = array.zip(nums)
pairs: Array[(String, Int)] = Array(([,2), (-,5), (],6))

scala> for((x,y)<-pairs) print(x*y)
[[-----]]]]]]

注意: 字符串與數字相乘,結果:字符串複製了該數字的倍數

2.8 partition操作

scala> val list = List(1,2,3,4,5,6,7,8,9,10)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> list.partition(_%2==0)
res21: (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9))

注意: partition函數的參數爲一個布爾表達式,返回值爲兩個集合,第一個集合爲滿足布爾表達式的值組成的集合,第二個集合爲不滿足布爾表達式的值組成的集合;兩個集合中元素的順序與原來保持一致。

2.9 flatten操作

scala> val list = List(List("a","b"),List("cc","dd"))
list: List[List[String]] = List(List(a, b), List(cc, dd))

scala> list.flatten
res22: List[String] = List(a, b, cc, dd)

2.10 flatMap操作

map和flatten操作的結合,先進行map操作,然後進行flatten操作

scala> val list = List(List("a","b"),List("cc","dd"))
list: List[List[String]] = List(List(a, b), List(cc, dd))

scala> list.flatMap(x=>x.map(_*2))
res24: List[String] = List(aa, bb, cccc, dddd)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章