集合
scala的集合有三大类:序列Seq,集Set,映射Map
所有的集合都扩展自Iterable特质
在scala中集合有可变(mutable)和不可变(immutable)两种类型,immutable类型的集合
初始化后就不能改变了(注意与val修饰的变量进行区别)
序列list
空的list是Nil
不可变的list
scala> val x = List(1, 2, 3, 4, 5, 6)
x: List[Int] = List(1, 2, 3, 4, 5, 6)
可变List
scala> val lb = scala.collection.mutable.ListBuffer(1, 2, 3, 4, 5)
lb: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5)
scala> val lst0 = scala.collection.mutable.ListBuffer[Int](_1_ 2_ 3)
lst0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
scala> val lst1 = new scala.collection.mutable.ListBuffer[Int]
lst1: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
//将lst1中的元素追加到lst0中,注意:没有生成新的集合
scala> lst0 ++= lst1
res4: lst0.type = ListBuffer(1, 2, 3, 4, 5)
//将lst0和lst1合并成一个新的ListBuffer. 注意:生成了一个新的集合
val lst2 = lst0 ++ lst1
//将元素追加到lst0的后面生成一个新的集合
val lst3 = lst0 :+ 5
添加元素
scala> lb += 7
res2: lb.type = ListBuffer(1, 2, 3, 4, 5, 7)
scala> lb += (8, 9, 10)
res3: lb.type = ListBuffer(1, 2, 3, 4, 5, 7, 8, 9, 10)
scala> lb ++= List(-1, -2)
res4: lb.type = ListBuffer(1, 2, 3, 4, 5, 7, 8, 9, 10, -1, -2)
//将0插入到lst1的前面生成一个新的List
scala> val lst1 = List(1, 2, 3)
lst1: List[Int] = List(1, 2, 3)
scala> val lst2 = 0 :: lst1
lst2: List[Int] = List(0, 1, 2, 3)
scala> val lst3 = lst1.::(0)
lst3: List[Int] = List(0, 1, 2, 3)
scala> val lst4 = 0 +: lst1
lst4: List[Int] = List(0, 1, 2, 3)
scala> val lst5 = lst1.+:(0)
lst5: List[Int] = List(0, 1, 2, 3)
//将一个元素添加到lst1的后面产生一个新的集合
scala> val lst6 = lst1 :+ 3
lst6: List[Int] = List(1, 2, 3, 3)
//合并两个list
scala> val lst0 = List(4, 5, 6)
lst0: List[Int] = List(4, 5, 6)
//将两个list合并成一个list
scala> val lst7 = lst1 ++ lst0
lst7: List[Int] = List(1, 2, 3, 4, 5, 6)
//将lst0加到前面
scala> val lst8 = lst0 ++: lst1
lst8: List[Int] = List(4, 5, 6, 1, 2, 3)
并集
scala> val l1 = List(5, 6, 7, 4)
l1: List[Int] = List(5, 6, 7, 4)
scala> val l2 = List(1, 2, 3, 4)
l2: List[Int] = List(1, 2, 3, 4)
scala> l1.union(l2)
res0: List[Int] = List(5, 6, 7, 4, 1, 2, 3, 4)
交集
scala> l1.intersect(l2)
res1: List[Int] = List(4)
差集
scala> l1.diff(l2)
res2: List[Int] = List(5, 6, 7)
更新
scala> val list1=List(1, 10)
list1: List[Int] = List(1, 10)
//产生新的集合
scala> list1.updated(1, 50)
res5: List[Int] = List(1, 50)
集Set
scala> val set1 = new scala.collection.immutable.HashSet[Int]()
set1: scala.collection.immutable.HashSet[Int] = Set()
//生成一个新的set
scala> set1 + 4
res0: scala.collection.immutable.HashSet[Int] = Set(4)
判断有没有
scala> val set1 = Set(1,2,3,4)
set1: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
scala> set1(5)
res9: Boolean = false
scala> set1.contains(7)
res10: Boolean = false
映射Map
定义,取值
在scala中,把哈希表这种数据结构叫做映射
没有顺序
定义定长map和不定长map
scala> val m = Map("a"->1, "b"->2, "c"->3)
m: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val x = Map("a"->1, "b"->2, "c"->3)
x: scala.collection.mutable.Map[String,Int] = Map(b -> 2, a -> 1, c -> 3)
取值
scala> x("a")
res2: Int = 1
scala> x("a")=2
取不到就给默认值
scala> m.getOrElse("d", -1)
res6: Int = -1
添加元素
scala> val m1 = scala.collection.mutable.Map(("a", 1), ("b", 2), ("c", 30))
m1: scala.collection.mutable.Map[String,Int] = Map(b -> 2, a -> 1, c -> 30)
scala> m1 += ("d" -> 1)
res0: m1.type = Map(b -> 2, d -> 1, a -> 1, c -> 30)
scala> m1 += (("e"->11), ("f"->232))
res1: m1.type = Map(e -> 11, b -> 2, d -> 1, a -> 1, c -> 30, f -> 232)
更新元素
scala> val map1 = Map("a"->1, "b"->2, "c"->3)
map1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
scala> map1.updated("a",111)
res7: scala.collection.immutable.Map[String,Int] = Map(a -> 111, b -> 2, c -> 3)
遍历
scala> for (m <- map1) println(m)
(a,1)
(b,2)
(c,3)
option
option
- some
- None
scala> val map1 = Map("a"->1, "b"->2, "c"->3)
map1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
scala> map1.get("a")
res0: Option[Int] = Some(1)
scala> map1.get("a").get
res2: Int = 1
scala> map1.get("ee")
res4: Option[Int] = None
拉链
import scala.collection.mutable
object MyPreDef extends App {
val zip1 = List("123", "456")
val zip2 = List("孙悟空", "猪八戒")
# 把2个zip压缩
val zipResultList = zip1 zip zip2
val zipMap = mutable.Map[String, String]()
for (e <- zipResultList) {
zipMap += e
}
println(zipMap)
}
输出
Map(456 -> 猪八戒, 123 -> 孙悟空)