当前位置: 代码迷 >> 综合 >> Scala中的集合:Iterator、BitSet、Set、Map、Stack、Vector、List、Array
  详细解决方案

Scala中的集合:Iterator、BitSet、Set、Map、Stack、Vector、List、Array

热度:12   发布时间:2023-12-16 05:33:23.0
??
5.    util

5.1.     架构

http://www.scala-lang.org/docu/files/collections-api/collections.html

The following figure shows all collections in package scala.collection. These are all high-level abstract classes or traits, which generally have mutable as well as immutable implementations.

The main trait is Iterable, which is the supertrait of both mutable and immutable variations of sequences (Seqs), sets, and maps. Sequences are ordered collections, such as arrays and lists. Sets contain at most one of each object, as determined by the == method. Maps contain a collection of keys mapped to values.

Sequences, classes that inherit from trait Seq, let you work with groups of data lined up in order. Because the elements are ordered, you can ask for the first element, second element, 103rd element, and so on.


 

scala.collection.immutable

The immutability helps you develop correct, efficient algorithms because you never need to make copies of a collection. 

scala.collection.mutable

 

不可变(collection.immutable._

可变(collection.mutable._

Array

ArrayBuffer

List

ListBuffer

String

StringBuilder

/

LinkedList, DoubleLinkedList

List

MutableList

/

Queue

Array

ArraySeq

Stack

Stack

HashMap HashSet

HashMap HashSet

 

ArrayStack

 

5.2.     集合Array,List,Tuple

Array

长度固定

元素可变

确定长度,后赋值;

List

长度固定

元素不可变

 

Tuple

长度固定

元素不可变

常用于有多个返回值的函数;或者多个变量的同时定义

Scala 2.8中,3者的元素都可以混合不同的类型(转化为Any类型);

Scala 2.7中,ArrayList都不能混合类型,只有Tuple可以;


Arraysallow you to hold a sequence of elements and efficiently access an element at an arbitrary position, both to get or update the element, with a zero-based index.

Listssupport fast addition and removal of items to the beginning of the list, but theydo not provide fast access to arbitrary indexes because the implementation must iterate through the list linearly.

5.2.1.  定义和初始化

5.2.1.1     Array

val list1 = new Array[String](0) // Array()

val list2 = new Array[String](3) // Array(null, null, null)

val list3:Array[String] = new Array(3) // // Array(null, null, null)

val list1 = Array("a","b","c","d") // 相当于Array.apply("a","b","c","d")

定义一个类型为AnyArray

val aa = Array[Any](1, 2)

或:

val aa: Array[Any] = Array(1, 2)

或:

val aa: Array[_] = Array(1, 2)

 

定义:

Array (1,3,5,7,9,11)

也可以用

Array[Int](1 to 11 by 2:_*)

暂时还没有找到Range(例如 1 to 11 by 2)之后跟:_*的依据

 

Array对应的可变ArrayBuffer

val ab = collection.mutable.ArrayBuffer[Int]()

ab += (1,3,5,7)

ab ++= List(9,11) // ArrayBuffer(1, 3, 5, 7, 9, 11)

ab toArray // Array (1, 3, 5, 7, 9, 11)

ab clear // ArrayBuffer()

5.2.1.2     List

val list:List[Int] = List(1,3,4,5,6) // 或者 List(1 to 6:_*)

val list1 = List("a","b","c","d") // 或者 List('a' to 'd':_*) map (_.toString)

元素合并进List::

val list2 = "a"::"b"::"c"::Nil // Nil是必须的

val list3 = "begin" :: list2 // list2不变,只能加在头,不能加在尾

多个List合并用++,也可以用:::(不如++)

val list4 = list2 ++ "end" ++ Nil

val list4 = list2 ::: "end" :: Nil // 相当于 list2 ::: List("end")

 

 import java.util._ 之后会产生冲突,需要指明包

scala.List(1,2,3)

 

List对应的可变ListBuffer

val lb = scala.collection.mutable.ListBuffer(1,2,3)

lb.append(4) // ListBuffer(1, 2, 3, 4)


val lb = collection.mutable.ListBuffer[Int]()

lb += (1,3,5,7)

lb ++= List(9,11) // ListBuffer(1, 3, 5, 7, 9, 11)

lb.toList // List(1, 3, 5, 7, 9, 11)

lb.clear // ListBuffer()

建议定义方式:

val head::body = List(4,"a","b","c","d")

// head: Any = 4

// body: List[Any] = List(a, b, c, d)

val a::b::c = List(1,2,3)

// a: Int = 1

// b: Int = 2

// c: List[Int] = List(3)

 

定义固定长度的List

List.fill(10)(2) // List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)

Array.fill(10)(2) // Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)

又如:

List.fill(10)(scala.util.Random.nextPrintableChar)

// List(?, =, ^, L, p, <, \, 4, 0, !)

List.fill(10)(scala.util.Random.nextInt(101))

// List(80, 45, 26, 75, 24, 72, 96, 88, 86, 15)


5.2.1.3     Tuple

val t1 = ("a","b","c")

var t2 = ("a", 123, 3.14, new Date())

val (a,b,c) = (2,4,6)

最简单的Tuple

1->"hello world"

和下面的写法是等价的:

(1, "hello world")


To access elements of a tuple, you can use method _1 to access the first element, _2 to access the second, and so on:

scala> val v = (1, "Nick", 43)

scala> v._1

res179: Int = 1

scala> v._2
res180: String = Nick

scala> v._3
res181: Int = 43

5.2.1.4     Vector

Scala2.8为了提高list的随机存取效率而引入的新集合类型(而list存取前部的元素快,越往后越慢)。

val v = Vector.empty

val v2 = 0 +: v :+ 10 :+ 20 // Vector(0, 10, 20), Vector 那一边始终有":"

v2(1) // 10

v2 updated (1,100) // Vector(0, 100, 20) 

这个例子举的不太好,scala.collection.immutable. Vector扩展、updated之后是新生成的vector,原vector保持immutable。这点和List类似。

Seq的缺省实现是List

Seq(1,2,3) // List(1, 2, 3)

IndexSeq的缺省实现是Vector:

IndexSeq(1,2,3) // Vector(1, 2, 3)

 

5.2.1.5     Range

Range(0, 5) // (0,1,2,3,4)

等同于:

0 until 5

等同于:

0 to 4

 

两个Range相加:

('0' to '9') ++ ('A' to 'Z') // (0,1,..,9,A,B,...,Z)

  相关解决方案