Scala教程之:可變和不變集合

集合在程序中是非常有用的,只有用好集合才能真正感受到該語言的魅力。在scala中集合主要在三個包里面:scala.collection, scala.collection.immutable和scala.collection.mutable。

scala中引入不可變集合是為了方便程序的使用并減少在程序中的未知風險。如果一個集合被定義為不可變的,那么我們在使用的過程中就可以指定該集合是不會變化的,可以放心使用。

我們看下這三個包的層次結構:

scala.collection的層次結構如下:

image.png

scala.collection.immutable的層次結構如下:

image.png

scala.collection.mutable的層次結構如下:

image.png

接下來我們通過兩個HashMap的例子來看一下immutable和mutable的使用。

mutable HashMap

我們看下怎么定義一個mutable hashMap :


  import scala.collection.mutable.HashMap
  println("\nStep 1: How to initialize a HashMap with 3 elements")
  val hashMap1: HashMap[String, String] = HashMap(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut"))
  println(s"Elements of hashMap1 = $hashMap1")

  println("\nStep 2: How to initialize HashMap using key -> value notation")
  val hashMap2: HashMap[String, String] = HashMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
  println(s"Elements of hashMap2 = $hashMap2")

怎么取出HashMap中的值:

  println("\nStep 3: How to access elements of HashMap by specific key")
  println(s"Element by key VD = ${hashMap2("VD")}")
  println(s"Element by key GD = ${hashMap2("GD")}")

怎么改變hashMap:

  println("\nStep 4: How to add elements to HashMap using +=")
  hashMap1 += ("KD" -> "Krispy Kreme Donut")
  println(s"Element in hashMap1 = $hashMap1")



  println("\nStep 5: How to add elements from a HashMap to an existing HashMap using ++=")
  hashMap1 ++= hashMap2
  println(s"Elements in hashMap1 = $hashMap1")



  println("\nStep 6: How to remove key and its value from HashMap using -=")
  hashMap1 -= "CD"
  println(s"HashMap without the key CD and its value = $hashMap1")

怎么定義一個空的HashMap:

  println("\nStep 7: How to initialize an empty HashMap")
  val emptyMap: HashMap[String,String] = HashMap.empty[String,String]
  println(s"Empty HashMap = $emptyMap")

immutable HashMap

看一下怎么定義一個immutable HashMap:

  import scala.collection.immutable.HashMap
  println("Step 1: How to initialize a HashMap with 3 elements using Tuples of key and value")
  val hashMap1: HashMap[String, String] = HashMap(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut"))
  println(s"Elements of hashMap1 = $hashMap1")



  println("\nStep 2: How to initialize HashMap using key -> value notation")
  val hashMap2: HashMap[String, String] = HashMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
  println(s"Elements of hashMap2 = $hashMap2")

獲取HashMap中的值:

  println("\nStep 3: How to access elements in HashMap by specific key")
  println(s"Element by key VD = ${hashMap2("VD")}")
  println(s"Element by key GD = ${hashMap2("GD")}")

我們再看一下怎么對集合進行操作,注意因為是immutable HashMap所以所有的操作都會返回一個新的HashMap:

  println("\nStep 4: How to add elements to HashMap using +")
  val hashMap3: HashMap[String, String] = hashMap1 + ("KD" -> "Krispy Kreme Donut")
  println(s"Element in hashMap3 = $hashMap3")



  println("\nStep 5: How to add two HashMaps together using ++")
  val hashMap4: Map[String, String] = hashMap1 ++ hashMap2
  println(s"Elements in hashMap4 = $hashMap4")



  println("\nStep 6: How to remove key and its value from HashMap using -")
  val hashMap5: Map[String, String] = hashMap4 - ("CD")
  println(s"HashMap without the key CD and its value = $hashMap5")

更多教程請參考 flydean的博客

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Scala的集合類可以從三個維度進行切分: 可變與不可變集合(Immutable and mutable coll...
    時待吾閱讀 5,951評論 0 4
  • 數(shù)組 :new Array[Int](8)與Array[Int](8)的區(qū)別:第一種8個元素,第二個定義一個值為8...
    夙夜M閱讀 1,867評論 1 2
  • 可變和不可變集合 Scala中的集合可分為可變集合和不可變集合。可變集合可以當場被更新,不可變集合本身是不可變的。...
    liqing151閱讀 320評論 0 0
  • 原文鏈接:https://github.com/EasyKotlin 本章將介紹Kotlin標準庫中的集合類,我們...
    JackChen1024閱讀 2,240評論 1 1
  • Scaladoc是什么:scala api文檔,包含了scala所有的api以及使用說明,class、object...
    義焃閱讀 2,735評論 0 1

友情鏈接更多精彩內容