美文网首页程序那些事Scala
Scala教程之:可变和不变集合

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

作者: flydean程序那些事 | 来源:发表于2020-01-08 14:22 被阅读0次

集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力。在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的博客

相关文章

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

    集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力。在scala中集合主要在三个包里面:scala....

  • Scala 集合与算子

    Scala 同时支持不可变集合和可变集合,不可变集合可以安全的并发访问,Scala 默认采用不可变集合。可变集合:...

  • Scala编程基础18:Scala集合

    Scala集合Set类型的元素是无序不重复的。Scala集合分为可变集合和不可变集合两种。默认情况下,Scala集...

  • Chapter 24《Collections in Depth》

    可变和不可变集合 Scala中的集合可分为可变集合和不可变集合。可变集合可以当场被更新,不可变集合本身是不可变的。...

  • Scala编程基础16:Scala集合概述

    Scala提供了一套很好用的集合实现,提供了一些集合类型的抽象。Scala集合分为可变結合和不可变集合。可变集合可...

  • scala学习笔记-容器

    可变和不可变(Scala默认不可变集合类) val和var mutable和immutable 补充string是...

  • 集合库-collection

    scala同时支持不可变集合和可变集合,因为不可变集合可以安全的并发访问,所以他也是默认使用的集合库类。在scal...

  • scala基础

    集合 Scala 集合分为可变的和不可变的集合 可变集合可以在适当的地方被更新或扩展。这意味着你可以修改,添加,移...

  • scala04.数据结构(重点)

    第4章数据结构(重点练习章节) 4.1主要的集合特质 4.1主要的集合特质Scala同时支持可变集合和不可变集合,...

  • 【Scala】Vector内部结构与内存共享原理

    Scala不可变集合 Scala不可变集合的设计目标是提供高效又安全的实现。这些集合中的大部分都是用高级技巧来在集...

网友评论

    本文标题:Scala教程之:可变和不变集合

    本文链接:https://www.haomeiwen.com/subject/lrcsactx.html