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

简介: 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")


相关文章
|
5月前
|
安全 Java Scala
Scala集合【上】
Scala集合【上】
|
6月前
|
Scala
162 Scala 集合
162 Scala 集合
21 0
|
5月前
|
分布式计算 Scala Spark
Scala【集合常用方法和函数操作(下)】
Scala【集合常用方法和函数操作(下)】
|
5月前
|
分布式计算 Scala Spark
Scala 【集合常用方法和函数操作-上】
Scala 【集合常用方法和函数操作-上】
|
3月前
|
Scala 容器
Scala学习--day04--集合、常用方法、案例实操 - WordCount TopN、不同省份的商品点击排行
Scala学习--day04--集合、常用方法、案例实操 - WordCount TopN、不同省份的商品点击排行
|
5月前
|
Scala
Scala综合练习_基于以下List集合实现词频统计
Scala综合练习_基于以下List集合实现词频统计
22 0
|
8月前
|
Scala 容器
Scala集合和遍历 2
Scala集合和遍历
42 0
|
8月前
|
Scala 索引
Scala集合和遍历 1
Scala集合和遍历
48 0
|
Java 大数据 Scala
大数据开发基础的编程语言的Scala的字符串/数组/集合
Scala是一种基于JVM的编程语言,它支持丰富的字符串、数组和集合操作。本文将介绍Scala中这些数据类型的概念和用法,帮助开发者更好地理解和应用这门语言。
58 0
Scala快速入门-11-常用集合操作
所有的集合都扩展自Iterable特质 集合有三大类,分别为序列、集和映射 几乎所有集合类,Scala都同时提供了可变和不可变的版本 Scala列表要么是空的,要么拥有一头一尾,其中尾部本身又是一个表列 集是无先后次序的集合 用LinkedHashSet来保留插入顺序,或用SortedSet来按顺序进行迭代 +将元素添加到无先后次序的集合中;+:和:+向前或向后追加到序列;++将两个集合串接在一起;-和--移除元素 映射、折叠和拉链操作是很有用的技巧,用来将函数和操作应用到集合中的元素