日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/2/28 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala教程之:可变和不变集合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • mutable HashMap
    • immutable HashMap

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

scala中引入不可變集合是為了方便程序的使用并減少在程序中的未知風(fēng)險(xiǎn)。如果一個(gè)集合被定義為不可變的,那么我們?cè)谑褂玫倪^(guò)程中就可以指定該集合是不會(huì)變化的,可以放心使用。

我們看下這三個(gè)包的層次結(jié)構(gòu):

scala.collection的層次結(jié)構(gòu)如下:

scala.collection.immutable的層次結(jié)構(gòu)如下:

scala.collection.mutable的層次結(jié)構(gòu)如下:

接下來(lái)我們通過(guò)兩個(gè)HashMap的例子來(lái)看一下immutable和mutable的使用。

mutable HashMap

我們看下怎么定義一個(gè)mutable hashMap :

import scala.collection.mutable.HashMapprintln("\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 ++= hashMap2println(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")

怎么定義一個(gè)空的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

看一下怎么定義一個(gè)immutable HashMap:

import scala.collection.immutable.HashMapprintln("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")}")

我們?cè)倏匆幌略趺磳?duì)集合進(jìn)行操作,注意因?yàn)槭莍mmutable HashMap所以所有的操作都會(huì)返回一個(gè)新的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 ++ hashMap2println(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")

更多精彩內(nèi)容且看:

  • 區(qū)塊鏈從入門(mén)到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
  • Spring Boot 2.X系列教程:七天從無(wú)到有掌握Spring Boot-持續(xù)更新
  • Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
  • java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程

更多教程請(qǐng)參考 flydean的博客

總結(jié)

以上是生活随笔為你收集整理的Scala教程之:可变和不变集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。