1. 可变容器通用方法(Common operators (methods) on mutable collections)
c += x:把x元素添加到集合c中
scala> val a = collection.mutable.ArrayBuffer(1,2,3)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> a += 4
res15: a.type = ArrayBuffer(1, 2, 3, 4)
c += (x,y,z):把x,y,z添加到集合c中
scala> val a = collection.mutable.ArrayBuffer(1,2,3)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> a += (4,5,6)
res16: a.type = ArrayBuffer(1, 2, 3, 4, 5, 6)
c1 ++= c2:把集合c2的元素全部添加到集合c1中
scala> val c1 = mutable.ArrayBuffer(1,2,3)
c1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> val c2 = mutable.ArrayBuffer(4,5,6)
c2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 5, 6)
scala> c1 ++= c2
res20: c1.type = ArrayBuffer(1, 2, 3, 4, 5, 6)
c -= x:在集合c中删除元素x
scala> val c = mutable.ArrayBuffer(1,2,3)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> c -= 3
res21: c.type = ArrayBuffer(1, 2)
scala> c -= 7
res22: c.type = ArrayBuffer(1, 2)
c -= (x,y,z):在集合c中删除元素x,y,z
scala> val c = mutable.ArrayBuffer(1,2,3,4,5)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> c -= (3,4)
res23: c.type = ArrayBuffer(1, 2, 5)
c1 --= c2:删除集合c1中所有在c2中存在的元素
scala> val c1 = mutable.ArrayBuffer(1,2,3,4,5)
c1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> val c2 = mutable.ArrayBuffer(2,4)
c2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 4)
scala> c1 --= c2
res24: c1.type = ArrayBuffer(1, 3, 5)
c(n) = x:把集合c中第n个元素赋值为x
scala> val c = mutable.ArrayBuffer(1,2,3,4,5)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> c(2) = 8
scala> c
res26: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 8, 4, 5)
c clear:删除集合c中的所有元素
scala> val c = mutable.ArrayBuffer(1,2,3,4,5)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> c.clear
scala> c
res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
c remove(n):在集合c中删除第n个位置的元素,并返回该元素
scala> val c = mutable.ArrayBuffer(1,2,3,4,5)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> c.remove(3)
res29: Int = 4
scala> c
res30: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 5)
c remove(n,len):在集合c的第n个元素开始,删除len个元素
scala> val c = mutable.ArrayBuffer(1,2,3,4,5)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> c.remove(1,2)
scala> c
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 4, 5)
2. 可变容器通用方法(Immutable collection operators)
下面我们来展示不可变容器的通用操作方法。不可变容器内容是不可变的,所以制定一个新的变量来接受这些方法的返回值。
c1 ++ c2:返回集合c1和集合c2的并集
scala> val v1 = Vector(1,2,3)
v1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
scala> val v2 = Vector(4,5,6)
v2: scala.collection.immutable.Vector[Int] = Vector(4, 5, 6)
scala> val v = v1 ++ v2
v: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5, 6)
c :+ e:返回一个新的集合,内容为集合c添加元素e形成的新集合
scala> Vector(1,2,3) :+ 4
res34: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4)
e +: c:返回一个新的集合,内容为元素e追加到集合c前面形成的新集合
scala> 4 +: Vector(3,2,1)
res35: scala.collection.immutable.Vector[Int] = Vector(4, 3, 2, 1)
e :: List:返回新的List,内容为元素e追加到List集合前形成的新List
scala> 4 :: List(3,2,1)
res37: List[Int] = List(4, 3, 2, 1)
来源:oschina
链接:https://my.oschina.net/u/2633112/blog/652154