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

本文深入介绍了Scala中的集合操作,包括mutable与immutable集合的概念,重点讲解了Set、Map的实战操作,以及元组和队列的使用。通过实例展示了如何在Scala中创建和使用这些数据结构,特别是如何处理Option类型以避免null异常。此外,还提及了栈操作,并提到了关注公众微信号获取更多Spark、Scala技术资讯。
摘要由CSDN通过智能技术生成

本节主要内容

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

mutable、immutable集合

以下内容来源于scala官方文档:
http://www.scala-lang.org/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
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值