Scala教程之:可变和不变集合
集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力。在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("\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的博客
Scala教程之:可变和不变集合的更多相关文章
- Scala教程之:深入理解协变和逆变
文章目录 函数的参数和返回值 可变类型的变异 在之前的文章中我们简单的介绍过scala中的协变和逆变,我们使用+ 来表示协变类型:使用-表示逆变类型:非转化类型不需要添加标记. 假如我们定义一个cla ...
- Scala教程之:Future和Promise
文章目录 定义返回Future的方法 阻塞方式获取Future的值 非阻塞方式获取Future的值 Future链 flatmap VS map Future.sequence() VS Future ...
- Scala教程之:函数式的Scala
文章目录 高阶函数 强制转换方法为函数 方法嵌套 多参数列表 样例类 比较 拷贝 模式匹配 密封类 单例对象 伴生对象 正则表达式模式 For表达式 Scala是一门函数式语言,接下来我们会讲一下几个 ...
- Scala教程之:面向对象的scala
文章目录 面向对象的scala Unified Types Classes Traits 面向对象的scala 我们知道Scala是一种JVM语言,可以合java无缝衔接,这也就大大的扩展了scala ...
- scala教程之:可见性规则
文章目录 public Protected private scoped private 和 scoped protected 和java很类似,scala也有自己的可见性规则,不同的是scala只有 ...
- Scala教程之:Either
在之前的文章中我们提到了Option,scala中Option表示存在0或者1个元素,如果在处理异常的时候Option就会有很大的限制,因为Option如果返回None,那么我并不知道具体的异常到底是 ...
- Scala教程之:PartialFunction
Scala中有一个很有用的traits叫PartialFunction,我看了下别人的翻译叫做偏函数,但是我觉得部分函数更加确切. 那么PartialFunction是做什么用的呢?简单点说Parti ...
- Scala教程之:Enumeration
Enumeration应该算是程序语言里面比较通用的一个类型,在scala中也存在这样的类型, 我们看下Enumeration的定义: abstract class Enumeration (init ...
- Scala教程之:Option-Some-None
文章目录 Option和Some Option和None Option和模式匹配 在java 8中,为了避免NullPointerException,引入了Option,在Scala中也有同样的用法. ...
随机推荐
- 剖析手写Vue,你也可以手写一个MVVM框架
剖析手写Vue,你也可以手写一个MVVM框架# 邮箱:563995050@qq.com github: https://github.com/xiaoqiuxiong 作者:肖秋雄(eddy) 温馨提 ...
- 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\vue.ps1,因为在此系统··········
网上百度了下,解决方案是: 1. 以管理员身份运行PowerShell2. 执行:get-ExecutionPolicy,回复Restricted,表示状态是禁止的3.执行:set-Execution ...
- 少儿编程Scratch第一讲:Scratch完美的初体验
素材及视频下载 链接:https://pan.baidu.com/s/1qX0T2B_zczcLaCCpiRrsnA提取码:xfp8 都说未来是人工智能.计算机程式控制的时代,如何让青少年接触计算机编 ...
- 关于Git我们不得不知道的事(一)
一.什么是Git? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git可以协助我们很方便的管理我们的项目,我们随时可以找回(或者回到)我们之前任何一个时刻的项目:还可以让同事或者开发小 ...
- ESLint如何配置
1.简介 通过用 ESLint 来检查一些规则,我们可以: 统一代码风格规则,如:代码缩进用几个空格:是否用驼峰命名法来命名变量和函数名等. 减少错误, 如:相等比较必须用 === ,变量在使用前必须 ...
- python--->相对和绝对路径
绝对路径(absolute path):从根开始找 eg:c:\file\01.txt 相对路径(relative path):相对当前文件内找 ../ # 当前文件的上一级 os.path ...
- Vue-router 第10节 路由中的钩子
Vue-router 第10节 路由中的钩子 [TOC] 第10节 路由中的钩子 我们知道一个组件从进入到销毁有很多的钩子函数,同样在路由中也设置了钩子函数.路由的钩子选项可以写在路由配置文件中,也可 ...
- vue中axios的安装使用
axios是一个基于 promise 的 HTTP 库,在vue中axios是比较常用的网络请求方法. 安装 npm install axios -S 在main.js配置 import axios ...
- hive常用函数四
字符串函数 1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abced ...
- python字符串列表元组序列操作
Table of Contents generated with DocToc python系列-字符串.列表.元组的操作 序列的访问及运算符 序列通用操作 访问单个元素 切片访问一部分元素 序列的复 ...