集合

scala的集合有三大类:序列Seq,集Set,映射Map
所有的集合都扩展自Iterable特质
在scala中集合有可变(mutable)和不可变(immutable)两种类型,immutable类型的集合
初始化后就不能改变了(注意与val修饰的变量进行区别)

序列list

空的list是Nil

不可变的list

  1. scala> val x = List(1, 2, 3, 4, 5, 6)
  2. x: List[Int] = List(1, 2, 3, 4, 5, 6)

可变List

  1. scala> val lb = scala.collection.mutable.ListBuffer(1, 2, 3, 4, 5)
  2. lb: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5)
  1. scala> val lst0 = scala.collection.mutable.ListBuffer[Int](_1_ 2_ 3)
  2. lst0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
  1. scala> val lst1 = new scala.collection.mutable.ListBuffer[Int]
  2. lst1: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
  1. //将lst1中的元素追加到lst0中,注意:没有生成新的集合
  2. scala> lst0 ++= lst1
  3. res4: lst0.type = ListBuffer(1, 2, 3, 4, 5)
  1. //将lst0和lst1合并成一个新的ListBuffer. 注意:生成了一个新的集合
  2. val lst2 = lst0 ++ lst1
  3. //将元素追加到lst0的后面生成一个新的集合
  4. val lst3 = lst0 :+ 5

添加元素

  1. scala> lb += 7
  2. res2: lb.type = ListBuffer(1, 2, 3, 4, 5, 7)
  3. scala> lb += (8, 9, 10)
  4. res3: lb.type = ListBuffer(1, 2, 3, 4, 5, 7, 8, 9, 10)
  5. scala> lb ++= List(-1, -2)
  6. res4: lb.type = ListBuffer(1, 2, 3, 4, 5, 7, 8, 9, 10, -1, -2)
  1. //将0插入到lst1的前面生成一个新的List
  2. scala> val lst1 = List(1, 2, 3)
  3. lst1: List[Int] = List(1, 2, 3)
  4. scala> val lst2 = 0 :: lst1
  5. lst2: List[Int] = List(0, 1, 2, 3)
  6. scala> val lst3 = lst1.::(0)
  7. lst3: List[Int] = List(0, 1, 2, 3)
  8. scala> val lst4 = 0 +: lst1
  9. lst4: List[Int] = List(0, 1, 2, 3)
  10. scala> val lst5 = lst1.+:(0)
  11. lst5: List[Int] = List(0, 1, 2, 3)
  1. //将一个元素添加到lst1的后面产生一个新的集合
  2. scala> val lst6 = lst1 :+ 3
  3. lst6: List[Int] = List(1, 2, 3, 3)
  1. //合并两个list
  2. scala> val lst0 = List(4, 5, 6)
  3. lst0: List[Int] = List(4, 5, 6)
  4. //将两个list合并成一个list
  5. scala> val lst7 = lst1 ++ lst0
  6. lst7: List[Int] = List(1, 2, 3, 4, 5, 6)
  7. //将lst0加到前面
  8. scala> val lst8 = lst0 ++: lst1
  9. lst8: List[Int] = List(4, 5, 6, 1, 2, 3)

并集

  1. scala> val l1 = List(5, 6, 7, 4)
  2. l1: List[Int] = List(5, 6, 7, 4)
  3. scala> val l2 = List(1, 2, 3, 4)
  4. l2: List[Int] = List(1, 2, 3, 4)
  5. scala> l1.union(l2)
  6. res0: List[Int] = List(5, 6, 7, 4, 1, 2, 3, 4)

交集

  1. scala> l1.intersect(l2)
  2. res1: List[Int] = List(4)

差集

  1. scala> l1.diff(l2)
  2. res2: List[Int] = List(5, 6, 7)

更新

  1. scala> val list1=List(1, 10)
  2. list1: List[Int] = List(1, 10)
  3. //产生新的集合
  4. scala> list1.updated(1, 50)
  5. res5: List[Int] = List(1, 50)

集Set

  1. scala> val set1 = new scala.collection.immutable.HashSet[Int]()
  2. set1: scala.collection.immutable.HashSet[Int] = Set()
  1. //生成一个新的set
  2. scala> set1 + 4
  3. res0: scala.collection.immutable.HashSet[Int] = Set(4)

判断有没有

  1. scala> val set1 = Set(1,2,3,4)
  2. set1: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
  3. scala> set1(5)
  4. res9: Boolean = false
  5. scala> set1.contains(7)
  6. res10: Boolean = false

映射Map

定义,取值

在scala中,把哈希表这种数据结构叫做映射
没有顺序

定义定长map和不定长map

  1. scala> val m = Map("a"->1, "b"->2, "c"->3)
  2. m: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
  3. scala> import scala.collection.mutable._
  4. import scala.collection.mutable._
  5. scala> val x = Map("a"->1, "b"->2, "c"->3)
  6. x: scala.collection.mutable.Map[String,Int] = Map(b -> 2, a -> 1, c -> 3)

取值

  1. scala> x("a")
  2. res2: Int = 1
  3. scala> x("a")=2

取不到就给默认值

  1. scala> m.getOrElse("d", -1)
  2. res6: Int = -1

添加元素

  1. scala> val m1 = scala.collection.mutable.Map(("a", 1), ("b", 2), ("c", 30))
  2. m1: scala.collection.mutable.Map[String,Int] = Map(b -> 2, a -> 1, c -> 30)
  3. scala> m1 += ("d" -> 1)
  4. res0: m1.type = Map(b -> 2, d -> 1, a -> 1, c -> 30)
  1. scala> m1 += (("e"->11), ("f"->232))
  2. res1: m1.type = Map(e -> 11, b -> 2, d -> 1, a -> 1, c -> 30, f -> 232)

更新元素

  1. scala> val map1 = Map("a"->1, "b"->2, "c"->3)
  2. map1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
  3. scala> map1.updated("a",111)
  4. res7: scala.collection.immutable.Map[String,Int] = Map(a -> 111, b -> 2, c -> 3)

遍历

  1. scala> for (m <- map1) println(m)
  2. (a,1)
  3. (b,2)
  4. (c,3)

option

option

  • some
  • None
  1. scala> val map1 = Map("a"->1, "b"->2, "c"->3)
  2. map1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
  3. scala> map1.get("a")
  4. res0: Option[Int] = Some(1)
  5. scala> map1.get("a").get
  6. res2: Int = 1
  7. scala> map1.get("ee")
  8. res4: Option[Int] = None

拉链

  1. import scala.collection.mutable
  2. object MyPreDef extends App {
  3. val zip1 = List("123", "456")
  4. val zip2 = List("孙悟空", "猪八戒")
  5. # 把2个zip压缩
  6. val zipResultList = zip1 zip zip2
  7. val zipMap = mutable.Map[String, String]()
  8. for (e <- zipResultList) {
  9. zipMap += e
  10. }
  11. println(zipMap)
  12. }

输出

  1. Map(456 -> 猪八戒, 123 -> 孙悟空)