本节主要内容
- mutable、immutable集合
- Set操作实战
- Map操作实战
- Tuple操作实战
- 队列操作实战
- 栈操作实战
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