Scala教程之:可变和不变集合
2023-04-18 13:10:32 时间
集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力。在scala中集合主要在三个包里面:scala.collection, scala.collection.immutable和scala.collection.mutable。
scala中引入不可变集合是为了方便程序的使用并减少在程序中的未知风险。如果一个集合被定义为不可变的,那么我们在使用的过程中就可以指定该集合是不会变化的,可以放心使用。
我们看下这三个包的层次结构:
scala.collection的层次结构如下:
scala.collection.immutable的层次结构如下:
scala.collection.mutable的层次结构如下:
接下来我们通过两个HashMap的例子来看一下immutable和mutable的使用。
mutable HashMap
我们看下怎么定义一个mutable hashMap :
import scala.collection.mutable.HashMap
println("
Step 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("
Step 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("
Step 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("
Step 4: How to add elements to HashMap using +=")
hashMap1 += ("KD" -> "Krispy Kreme Donut")
println(s"Element in hashMap1 = $hashMap1")
println("
Step 5: How to add elements from a HashMap to an existing HashMap using ++=")
hashMap1 ++= hashMap2
println(s"Elements in hashMap1 = $hashMap1")
println("
Step 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("
Step 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("
Step 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("
Step 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("
Step 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("
Step 5: How to add two HashMaps together using ++")
val hashMap4: Map[String, String] = hashMap1 ++ hashMap2
println(s"Elements in hashMap4 = $hashMap4")
println("
Step 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的博客
相关文章
- 家中狗子都在想些啥,科学家们利用机器学习搞清楚了
- 5G 就绪:赋能技术为未来工作做好准备
- 八种时间序列分类方法总结
- 瑞数信息再次入选Gartner《2022年中国ICT技术成熟度曲线报告》云安全示例厂商
- kubernetes生产环境优秀实践
- 智能家庭需要更智能的Wi-Fi
- Kubernetes网络插件详解 - Calico篇 - 网络基础
- 帕累托排序学习:基于推荐系统公平性的排序学习
- 亚马逊谷歌炮轰微软云计算调整:限制客户自由流动
- 多模态算法在视频理解中的应用
- LoRaWAN 和物联网在优化资产管理中的作用
- 年终访谈:人工智能技术与产业的发展变革
- 云:数字创新的未来
- 2022年实施混合云和多云战略需要考虑的关键因素
- 以太网分离器会降低网速吗?
- 温度补偿:补偿的其实不是温度?
- 详解:什么是网络交换机?
- 我敢打赌,看完这个故事,你一定能搞懂“算力网络”……
- 大模型推动的人机交互对话
- 常见的企业网络连接类型