Scala—— Set、Map、Tuple、佇列操作實戰
本節主要內容
- mutable、immutable集合
- Set操作實戰
- Map操作實戰
- Tuple操作實戰
- 佇列操作實戰
- 棧操作實戰
mutable、immutable集合
以下內容來源於scala官方文件:
http://www.scala-lang.org/docu/files/collections-api/collections.html
Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or extended in place.
This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change. You have still operations that simulate additions, removals, or updates,
but those operations will in each case return a new collection and leave the old collection unchanged.
//大致意思是:scala中的集合分為兩種,一種是可變的集合,另一種是不可變的集合
//可變的集合可以更新或修改,新增、刪除、修改元素將作用於原集合
//不可變集合一量被建立,便不能被改變,新增、刪除、更新操作返回的是新的集合,老集合保持不變
- 1
- 2
- 3
- 4
- 5
- 6
scala中所有的集合都來自於scala.collection包及其子包mutable, immutable當中
//scala.collection.immutable包中的集合絕對是不可變的,函數語言程式設計語言推崇使用immutable集合
A collection in package scala.collection.immutable is guaranteed to be immutable for everyone. Such a collection will never change after it is created.
Therefore, you can rely on the fact that accessing the same collection value repeatedly at different points in time will always yield a collection with the same elements.
//scala.collection.immutable包中的集合在是可變的,使用的時候必須明白集合何時發生變化
A collection in package scala.collection.mutable is known to have some operations that change the collection in place.
So dealing with mutable collection means you need to understand which code changes which collection when.
//scala.collection中的集合要麼是mutalbe的,要麼是immutable的
//同時該包中也定義了immutable及mutable集合的介面
A collection in package scala.collection can be either mutable or immutable. For instance,
collection.IndexedSeq[T] is a superclass of both collection.immutable.IndexedSeq[T] and collection.mutable.IndexedSeq[T] Generally,
the root collections in package scala.collection define the same interface as the immutable collections,
and the mutable collections in package scala.collection.mutable typically add some side-effecting modification operations to this immutable interface.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
在scala中,預設使用的都是immutable集合,如果要使用mutable集合,需要在程式中引入
import scala.collection.mutable
//由於immutable是預設匯入的,因此要使用mutable中的集合的話
//使用如下語句
scala> val mutableSet=mutable.Set(1,2,3)
mutableSet: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
//不指定的話,建立的是immutable 集合
scala> val mutableSet=Set(1,2,3)
mutableSet: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
直接使用Set(1,2,3)建立的是immutable集合,這是因為當你不引入任何包的時候,scala會預設匯入以幾個包:
Predef物件中包含了Set、Map等的定義
scala集合類的層次結構:
scala.collection包中的集合類層次結構如下圖:
These are all high-level abstract classes or traits, which generally have mutable as well as immutable implementations.
scala.collection.immutable包中的類層次結構:
scala.collection.mutable包中的類層次結構:
可變集合與不可變集合對應關係:
Set操作實戰
1 Set(集)是一種不存在重複元素的集合,它與數學上定義的集合是對應的
//定義一個集合
//這裡使用的是mutable
scala> val numsSet=Set(3.0,5)
numsSet: scala.collection.mutable.Set[Double] = Set(5.0, 3.0)
//向集中新增一個元素,同前一講中的列表和陣列不一樣的是
//,Set在插入元素時並不保元素的順序
//預設情況下,Set的實現方式是HashSet實現方式,
//集中的元素通過HashCode值進行組織
scala> numsSet+6
res20: scala.collection.mutable.Set[Double] = Set(5.0, 6.0, 3.0)
//遍歷集
scala> for ( i <- res20 ) println(i)
5.0
6.0
3.0
//如果對插入的順序有著嚴格的要求,則採用scala.collection.mutalbe.LinkedHashSet來實現
scala> val linkedHashSet=scala.collection.mutable.LinkedHashSet(3.0,5)
linkedHashSet: scala.collection.mutable.LinkedHashSet[Double] = Set(3.0, 5.0)
scala> linkedHashSet+6
res26: scala.collection.mutable.LinkedHashSet[Double] = Set(3.0, 5.0, 6.0)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
Map操作實戰
Map是一種鍵值對的集合,一般將其翻譯為對映
//直接初始化
// ->操作符,左邊是key,右邊是value
scala> val studentInfo=Map("john" -> 21, "stephen" -> 22,"lucy" -> 20)
studentInfo: scala.collection.immutable.Map[String,Int] = Map(john -> 21, stephe
n -> 22, lucy -> 20)
//immutable不可變,它不具有以下操作
scala> studentInfo.clear()
<console>:10: error: value clear is not a member of scala.collection.immutable.M
ap[String,Int]
studentInfo.clear()
^
//建立可變的Map
scala> val studentInfoMutable=scala.collection.mutable.Map("john" -> 21, "stephe
n" -> 22,"lucy" -> 20)
studentInfoMutable: scala.collection.mutable.Map[String,Int] = Map(john -> 21, l
ucy -> 20, stephen -> 22)
//mutable Map可變,比如可以將其內容清空
scala> studentInfoMutable.clear()
scala> studentInfoMutable
res3: scala.collection.mutable.Map[String,Int] = Map()
//遍歷操作1
scala> for( i <- studentInfoMutable ) println(i)
(john,21)
(lucy,20)
(stephen,22)
//遍歷操作2
scala> studentInfoMutable.foreach(e=>
{val (k,v)=e; println(k+":"+v)}
)
john:21
lucy:20
stephen:22
//遍歷操作3
scala> studentInfoMutable.foreach(e=> println(e._1+":"+e._2))
john:21
lucy:20
stephen:22
//定義一個空的Map
scala> val xMap=new scala.collection.mutable.HashMap[String,Int]()
xMap: scala.collection.mutable.HashMap[String,Int] = Map()
//往裡面填充值
scala> xMap.put("spark",1)
res12: Option[Int] = None
scala> xMap
res13: scala.collection.mutable.HashMap[String,Int] = Map(spark -> 1)
//判斷是否包含spark字串
scala> xMap.contains("spark")
res14: Boolean = true
//-> 初始化Map,也可以通過 ("spark",1)這種方式實現(元組的形式)
scala> val xMap=scala.collection.mutable.Map(("spark",1),("hive",1))
xMap: scala.collection.mutable.Map[String,Int] = Map(spark -> 1, hive -> 1)
scala> "spark" -> 1
res18: (String, Int) = (spark,1)
//獲取元素
scala> xMap.get("spark")
res19: Option[Int] = Some(1)
scala> xMap.get("SparkSQL")
res20: Option[Int] = None
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
Option,None,Some型別
Option、None、Some是scala中定義的型別,它們在scala語言中十分常用,因此這三個型別非學重要。
None、Some是Option的子類,它主要解決值為null的問題,在java語言中,對於定義好的HashMap,如果get方法中傳入的鍵不存在,方法會返回null,在編寫程式碼的時候對於null的這種情況通常需要特殊處理,然而在實際中經常會忘記,因此它很容易引起 NullPointerException異常。在Scala語言中通過Option、None、Some這三個類來避免這樣的問題,這樣做有幾個好處,首先是程式碼可讀性更強,當看到Option時,我們自然而然就知道它的值是可選的,然後變數是Option,比如Option[String]的時候,直接使用String的話,編譯直接通不過。
前面我們看到:
scala> xMap.get("spark")
res19: Option[Int] = Some(1)
- 1
- 2
那要怎麼才能獲取到最終的結果呢,
//通過模式匹配得到最終的結果
scala> def show(x:Option[Int]) =x match{
| case Some(s) => s
| case None => “????”
| }
show: (x: Option[Int])Any
scala> show(xMap.get(“spark”))
res21: Any = 1
scala> show(xMap.get(“sparkSQL”))
res22: Any = ????
元組操作實戰
前面我們提到Map是鍵值對的集合,元組則是不同型別值的聚集
//元組的定義
scala> ("hello","china","beijing")
res23: (String, String, String) = (hello,china,beijing)
scala> ("hello","china",1)
res24: (String, String, Int) = (hello,china,1)
scala> var tuple=("Hello","China",1)
tuple: (String, String, Int) = (Hello,China,1)
//訪問元組內容
scala> tuple._1
res25: String = Hello
scala> tuple._2
res26: String = China
scala> tuple._3
res27: Int = 1
//通過模式匹配獲取元組內容
scala> val (first, second, third)=tuple
first: String = Hello
second: String = China
third: Int = 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
佇列操作實戰
//immutable queue
scala> var queue=scala.collection.immutable.Queue(1,2,3)
queue: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)
//出隊
scala> queue.dequeue
res38: (Int, scala.collection.immutable.Queue[Int]) = (1,Queue(2, 3))
//入隊
scala> queue.enqueue(4)
res40: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4)
//mutable queue
scala> var queue=scala.collection.mutable.Queue(1,2,3,4,5)
queue: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5)
//入隊操作
scala> queue += 5
res43: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 5)
//集合方式
scala> queue ++= List(6,7,8)
res45: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 5, 6, 7, 8)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
棧操作實戰
//mutable Stack
scala> import scala.collection.mutable.Stack
import scala.collection.mutable.Stack
//new 建立方式
scala> val stack = new Stack[Int]
stack: scala.collection.mutable.Stack[Int] = Stack()
//Apply建立方式
scala> val stack1=Stack(1,2,3)
stack1: scala.collection.mutable.Stack[Int] = Stack(1, 2, 3)
//出棧
scala> stack1.top
res55: Int = 1
//入棧
scala> stack.push(1)
res57: stack.type = Stack(1)
//入棧
scala> stack.push(2)
res58: stack.type = Stack(2, 1)
//出棧
scala> stack.top
res59: Int = 2
scala> stack
res60: scala.collection.mutable.Stack[Int] = Stack(2, 1)
相關文章
- 【Scala篇】--Scala中集合陣列,list,set,map,元祖陣列
- Scala中的Map、Tuple、Zip
- Scala操作Map
- Scala與Java差異(五)之Map與TupleJava
- 使用陣列實現環形佇列Scala版本陣列佇列
- Scala學習1.2 Scala中Array Map等資料結構實戰資料結構
- Scala陣列操作陣列
- scala常用操作-Tuple元祖轉換成String字串字串
- Scala的陣列操作陣列
- RabbitMQ實戰《延遲佇列》MQ佇列
- scala佇列、並行集合基本使用佇列並行
- 兩個棧實現佇列操作佇列
- Laravel Queues 佇列應用實戰Laravel佇列
- List ,Set,Map集合與陣列互轉陣列
- 陣列的reduce操作+物件陣列的map操作陣列物件
- Laravel 佇列基本操作Laravel佇列
- 佇列的基本操作佇列
- STL醜數(set+優先佇列)佇列
- 實驗四 棧和佇列的基本操作佇列
- 分散式佇列程式設計:模型、實戰分散式佇列程式設計模型
- 順序佇列基本操作佇列
- Python 通過List 實現佇列的操作Python佇列
- Java List/Set/MapJava
- List、Set、Queue、Map
- Python演算法實戰系列之佇列Python演算法佇列
- Redis 分散式鎖與任務佇列實戰Redis分散式佇列
- Python 的List 和tuple,Dict,SetPython
- ES6的Set、Map資料結構 陣列資料結構陣列
- 迴圈佇列的基本操作佇列
- 實戰PHP資料結構基礎之佇列PHP資料結構佇列
- java Map Set遍歷Java
- List,Set,Queue,Map介面
- Java中 set,list,array(集合與陣列)、Map相互轉換Java陣列
- 佇列的一種實現:迴圈佇列佇列
- 通過佇列實現棧OR通過棧實現佇列佇列
- 資料結構實驗5、鏈佇列的基本操作資料結構佇列
- javascript實現佇列JavaScript佇列
- Python佇列的三種佇列實現方法Python佇列