scala中的集合框架
Scala的集合框架
Scala把集合框架分為3種:
1.序列:Seq
2.集:Set
3.對映:Map iterable
每一個集合都有兩個包:根據包進行區別可變和不可變
1.不可變集合 scala.collection.immutable 預設的,不需要匯入的
2.可變集合 scala.collecion.mutable.*
重點:元組,陣列 ,List,Map,Set
元組 非常靈活
可以儲存不同型別的引數
1.元組使用()封裝
(1)如何建立:new Tuple(n)
(2)n:元組中儲存元素的個數,但是最多隻能儲存22個
(3)元組下標從1開始
(4)元組的取值
①例如val tp2=new Tuple3(1,”哈哈”,2.4)
②取值:tp2._1(1),tp2._2(哈哈),tp2._3(2.4)
2.對偶元組
例如:val tup2=(“name”,20)
Map(k,v)中的每一個元素,就是對偶元組
元組的元素交換:tup2.swap
3.拉鍊操作:zip zipWithIndex
例如:
scala> val teacher=Array("haiyuan","lichen","shilaoshi") teacher: Array[String] = Array(haiyuan, lichen, shilaoshi) scala> val tizhong=Array(100,300,200,120) tizhong: Array[Int] = Array(100, 300, 200, 120) scala> val nameAndtizhong=teacher.zip(tizhong) nameAndtizhong: Array[(String, Int)] = Array((haiyuan,100), (lichen,300), (shilaoshi,200)) scala> nameAndtizhong.zipWithIndex res8: Array[((String, Int), Int)] = Array(((haiyuan,100),0), ((lichen,300),1), ((shilaoshi,200),2)) 巢狀的對偶元組
4.取巢狀三組中的元素
scala> val teacher=Array("haiyuan","lichen","shiguangxin") teacher: Array[String] = Array(haiyuan, lichen, shiguangxin) scala> val weight=Array(100,180,150,120) weight: Array[Int] = Array(100, 180, 150, 120) scala> val nameAndWeight=teacher.zip(weight) nameAndWeight: Array[(String, Int)] = Array((haiyuan,100), (lichen,180), (shiguangxin,150)) scala> nameAndWeight.zipWithIndex res0: Array[((String, Int), Int)] = Array(((haiyuan,100),0), ((lichen,180),1), ((shiguangxin,150),2)) //取Array中的第一個 scala> res0(0) res2: ((String, Int), Int) = ((haiyuan,100),0) //取巢狀元組中的第一個 scala> res2._1 res3: (String, Int) = (haiyuan,100) //取元組的第一個元素 scala> res3._1 res6: String = haiyuan
陣列
陣列綜述:
1.不可變陣列:Array:長度不可變,內容可以變化
2.可變陣列:
(1)ArrayBuffer:長度和內容都可以變化
(2)導包:scala.collection.mutable.ArrayBuffer
陣列的定義方式
Array
1.Val arr:Array[Double]=Array(11.2,3.2)
(1)取值:arr(0)
(2)賦值:arr(0)=10.0
①賦值已有的元素,元素會被覆蓋
2.Val arr2:Array[Int]=new ArrayInt
scala> val arr=new Array(4)
arr: Array[Nothing] = Array(null, null, null, null)
注意:
1.Nothing是報錯的標識
2.Null是空的意思
ArrayBuffer(ArrayBuffer:必須導包)
scala>import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
//arrayBuffer新增或者刪除元素:
scala> val arr=ArrayBuffer(1,2,3,4,7,8)
arr: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 7, 8)
scala>arr+=5
res7: arr.type = ArrayBuffer(1, 2, 3, 4, 7, 8, 5)
scala> arr++=Array(6,9)
res8: arr.type = ArrayBuffer(1, 2, 3, 4, 7, 8, 5, 6, 9)
scala> arr-=5
res9: arr.type = ArrayBuffer(1, 2, 3, 4, 7, 8, 6, 9)
scala>arr--=Array(6,8)
res10: arr.type = ArrayBuffer(1, 2, 3, 4, 7, 9)
//插入元素:insert(n:Int,m:Int*)
//插入元素的時候,插入在指定索引的前面
scala>res10.insert(5,6)
scala>res10
res13: arr.type = ArrayBuffer(1, 2, 3, 4, 7, 6, 9)
//移除元素:remove(n:Int,m:Int)
//移除元素是指定索引後幾位數字
scala>res13.remove(2,3)
scala>res13
res15: arr.type = ArrayBuffer(1, 2, 6, 9)
//清空陣列:clear
scala> res13.clear
scala>res13
res18: arr.type = ArrayBuffer()
//判斷是否為空:isEmpty
scala> res13.isEmpty
res19: Boolean = true
scala>ab1.in注意需要按Tab,而不是Enter
indexOf indexOfSlice indexWhere indices init inits insert insertAll intersect
陣列常用的方法
Max:最大值
Min:最小值
Sum:求和
Sorted:排序
Reverse:反轉
scala> ab1.max
res29: Int = 5
scala> ab1.min
res30: Int = 2
scala>ab1.sum
res31: Int = 13
scala> ab1.sorted
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 3, 3, 5)
scala> ab1.sorted.reverse
res33: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5, 3, 3, 2)
List
1.不可變List:長度和內容都不可變,預設的
2.可變的List:
(1)ListBuffer長度和內容都可變
(2)導包:scala.collection.mutable.ListBuffer
不可變的List
//定義方式:
scala> val list=List[Int](1,2,3,6,7)
list: List[Int] = List(1, 2, 3, 6, 7)
scala> val list1=6::4::8::Nil
list1: List[Int] = List(6, 4, 8)
3.::Nil是組成List的兩個單位
Nil代表空列表
scala> List[Int]()==Nil
res1: Boolean = true
4.::方法(新增元素放在首位)
scala> 5::list
res2: List[Int] = List(5, 1, 2, 3, 6, 7)
可變的ListBuffer
//定義方式
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
scala> val list1=ListBuffer[Int](1,2,3)
list1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
//集合基本操作
+=
++=
-+
–+
Insert
Remove
Clear
isEmpty
scala>list1+=13
res3: list1.type = ListBuffer(1, 2, 3, 13)
scala> list1++=List(12,4,5,6)
res6: list1.type = ListBuffer(1, 2, 3, 13, 12, 4, 5, 6)
scala> list1.insert(7)
scala>list1
res9: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 13, 12, 4, 5, 6)
scala>list1.remove(1)
res10: Int = 2
scala>list1.clear
scala> list1
res14: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
集合間的轉換
可變List:
1.通過toList()方法,轉換為不可變的List
2.toBuffer 可以轉化為ArrayBuffer
注意:轉換後,是新的集合,不是之前的那個集合
scala> list1.toList
res20: List[Int] = List(12, 11, 1, 2)
scala>* list1.toBuffer
res21: scala.collection.mutable.Buffer[Int] = ArrayBuffer(12, 11, 1, 2)
頭元素和尾元素
1.一個List元素,分為兩部分:頭元素(head)+尾元素(tail)
2.頭元素:第一個元素
3.尾元素:除了第一個元素的其他元素都是尾元素
scala> val list=List(1,3,4,6,7)
list: List[Int] = List(1, 3, 4, 6, 7)
scala> list.head
res22: Int = 1
scala> list.tail
res23: List[Int] = List(3, 4, 6, 7)
scala> res23.tail
res24: List[Int] = List(4, 6, 7)
scala> res23.tail.tail
res25: List[Int] = List(6, 7)
Map
1.不可變的Map,預設的
2.可變的Map,導包:scala.collection.mutiable.Map
3.Map中存在的元素,都是對偶元素
4.Key是唯一的
不可變map
def main(args: Array[String]): Unit = {
// 定義形式
val mp: Map[String, Int] = Map[String, Int]("haiyuan" -> 100, "lichen" -> 300)
val mp2: mutable.HashMap[String, Int] = new mutable.HashMap[String, Int]()
// 兩個map能和並嗎?
val MP3 = mp ++ mp2
// 取值,跟據k取V
val m1: Option[Int] = mp.get("haiyuan")
// option some (有值) None(取不到值)
// println(mp.get("haiyuan1"))
val else1: Int = mp.getOrElse("haiyuan1", 100)
println(else1)
}
可變map(導包)
def main(args: Array[String]): Unit = {
import scala.collection.mutable.Map
import scala.collection.mutable.HashMap
val mmp: mutable.Map[String, Int] = Map[String, Int]()
// 如何新增元素
val mmp3: Option[Int] = mmp.put("haiyuan", 100)
// 還能如何新增
mmp += ("chuanwang" -> 99)
// 一下新增兩個元素
mmp += (("lichen", 300), ("shilaoshi", 120))
}
}
Map迭代key和value
def main(args: Array[String]): Unit = {
val mp1: Map[String, Int] = Map[String, Int]("haiyuan" -> 120, "licheng" -> 175, "huahua" -> 88)
for (i <- mp1) {
println(i._1, i._2)
println(i._1)
println(i._2)
}
for ((k, v) <- mp1) {
println(k, v)
println(k)
println(v)
}
for ((k, _) <- mp1) {
println(k)
}
for ((_, v) <- mp1) {
println(v)
}
val ks: Iterator[String] = mp1.keysIterator
//如何遍歷
while (ks.hasNext) {
println(ks.next())
}
//還能怎麼做
mp1.foreach(println)
ks.foreach(println)
//迭代v
val values: Iterable[Int] = mp1.values
//遍歷方式
values.foreach(println)
}
Set
集合,元素不能重複
1.不可變,長度,內容都不可變
2.可變:
(1)長度,內容都可變
(2)導包:scala.collection.mutable.Set
不可變set
scala> val set=Set(1,2,3,3,4,2,4,5)
set: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
可變set
scala> import scala.collection.mutable.Set
import scala.collection.mutable.Set
scala> val set=Set[Int](10)
set: scala.collection.mutable.Set[Int] = Set(10)
scala> set+=12
res26: set.type = Set(12, 10)
scala> set+=11
res27: set.type = Set(12, 10, 11)
//不重複
scala> set+=11
res28: set.type = Set(12, 10, 11)
scala> set+=(11,23,45)
res29: set.type = Set(12, 45, 10, 11, 23)
scala> set++=Set(111,222)
res31: set.type = Set(12, 45, 222, 111, 10, 11, 23)
scala> set.remove(11)
res32: Boolean = true
scala> set.remove(1)
res33: Boolean = false
scala> set.head
res36: Int = 12
scala> set.tail
res37: scala.collection.mutable.Set[Int] = Set(45, 222, 111, 10, 23)
scala> set.foreach(println)
12
45
222
111
10
23
//set本身用的不多,但是我們要去重的時候,就可以用到
scala> val list=List(11,22,33,11,22,12,123,123,444)
list: List[Int] = List(11, 22, 33, 11, 22, 12, 123, 123, 444)
scala> val list1=list.toSet
list1: scala.collection.immutable.Set[Int] = Set(33, 444, 22, 12, 123, 11)
scala> list1.toList
res40: List[Int] = List(33, 444, 22, 12, 123, 11)
運算子過載的方法
scala> 3+2
res42: Int = 5
scala> 2+(4)
res43: Int = 6
scala> 1.to(10)
res44: scala.collection.immutable.Range.Inclusive = Range 1 to 10
scala> val list=List(2,3,4,51,55)
list: List[Int] = List(2, 3, 4, 51, 55)
scala> list.head
res45: Int = 2
scala> list head
res47: Int = 2
scala> list.tail
res46: List[Int] = List(3, 4, 51, 55)