Scala拥有丰富的集合类型库。
scala集合类型的类层级
Iterable是主要特质,它同时还是可变和不可变序列(Seq)、集(Set),以及映射(Map)的超特质。
命名为Iterable是为了说明集合对象可以通过名为elements的方法产生为Iterator(枚举器):
def elements: Iterator[A]
Iterator(枚举器)的类层级
序列
序列是继承自特质Seq的类,它可以让你处理一组线性分布的数据。
//列表
val colors = List("red", "blue", "green")
//colors: List[String] = List(red, blue, green)
colors.head
//res83: String = red
colors.tail
//res84: List[String] = List(blue, green)
//数组
val fiveToOne = Array(5, 4, 3, 2, 1)
//fiveToOne: Array[Int] = Array(5, 4, 3, 2, 1)
val fiveInts = new Array[Int](5)
//fiveInts: Array[Int] = Array(0, 0, 0, 0, 0)
fiveInts(0) = fiveToOne(4)
fiveInts
//res87: Array[Int] = Array(1, 0, 0, 0, 0)
//列表缓存
import scala.collection.mutable.ListBuffer
val buf = new ListBuffer[Int]
buf += 1
buf += 2
buf += 2
buf
//res91: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 2)
3 +: buf
//res92: scala.collection.mutable.ListBuffer[Int] = ListBuffer(3, 1, 2, 2)
buf
//3没有加进去??res93: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 2)
buf.toList
//res94: List[Int] = List(1, 2, 2)
//数组缓存
import scala.collection.mutable.ArrayBuffer
val buf = new ArrayBuffer[Int]()
buf += 12
buf += 15
buf
//res99: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(12, 15)
buf.length
//res100: Int = 2
buf(0)
//res101: Int = 12
//队列
//不可变队列
import scala.collection.immutable.Queue
val empty = Queue[Int]()
//使用enqueue为不可变队列添加元素
val has1 = empty.enqueue(1)
//添加元素,可以把集合当作enqueue调用的参数
val has123 = has1.enqueue(List(2, 3))
//从队列的头部移除元素,可以使用dequeue
val (element, has23) = has123.dequeue
//element: Int = 1
//has23: scala.collection.immutable.Queue[Int] = Queue(2, 3)
//可变队列
import scala.collection.mutable.Queue
val queue=Queue[String]()
queue+="a"
queue ++= List("b", "c")
queue
//res107: scala.collection.mutable.Queue[String] = Queue(a, b, c)
queue.dequeue
//res108: String = a
queue
//res109: scala.collection.mutable.Queue[String] = Queue(b, c)
//如果需要的是后进先出的序列,可以使用栈,元素推入使用push,弹出使用pop,获取栈顶元素而不移除使用top
import scala.collection.mutable.Stack
val stack =Stack[Int]()
stack.push(1)
//res110: stack.type = Stack(1)
stack.push(2)
//res111: stack.type = Stack(2, 1)
stack.pop
//res112: Int = 2
stack.pop
//res112: Int = 2
stack.top
//res113: Int = 1
关于大小写判断
val s="String"
//s: String = String
s.toUpperCase
//res116: String = STRING
s.toUpperCase==s
//res117: Boolean = false布尔型
集
集的关键特性在于可以使用对象的==操作,确保任何时候每个对象在集中都只保留一个副本
import scala.collection.mutable
val mutaSet = mutable.Set(1, 2, 3)
val text = "See Spot run. Run, Spot. Run!"
val wordsArray = text.split("[ !,.]+")
val words = mutable.Set.empty[String]
for (word <- wordsArray) words += word.toLowerCase
words
//res2: scala.collection.mutable.Set[String] = Set(see, run, spot)
集的常用操作
映射
映射可以把值和集合中的元素联系起来。
//创建类型的时候,指定key和value的类型
val map = mutable.Map.empty[String, Int]
map("hello") = 1
map("there") = 2
map
//res5: scala.collection.mutable.Map[String,Int] = Map(hello -> 1, there -> 2)
map("hello")
//res6: Int = 1
词频统计
def countWords(text:String)={
val counts=mutable.Map.empty[String,Int]
for (rawWord <- text.split("[,!.]+")){
val word=rawWord.toLowerCase
val oldCount = if(counts.contains(word)) counts(word) else 0
counts += (word->(oldCount+1))
}
counts
}
countWords("See Spot run! Run, Spot. Run!")
//res7: scala.collection.mutable.Map[String,Int] = Map(" run" -> 2, " spot" -> 1, see spot run -> 1)
映射的常用操作
映射的常用操作 映射的常用操作(续)
元组
元组可以把固定数量的元素组合到一起整体传输
def longestWord(words: Array[String]) = {
var word = words(0)
var idx = 0
for (i <- 1 until words.length)
if (words(i).length > word.length) {
word = words(i)
idx = i
}
(word, idx)
}
//longestWord: (words: Array[String])(String, Int)
val longest = longestWord("The quick brown fox".split(" "))
//longest: (String, Int) = (quick,1)
longest._1
//res12: String = quick
longest._2
//res13: Int = 1
val (word, idx) = longest
//word: String = quick
//idx: Int = 1
val word, idx = longest
//word: (String, Int) = (quick,1)
//idx: (String, Int) = (quick,1)
网友评论