第十章 Scala 容器(三):使用容器通用方法解決問題

1. 綜述

    Scala的容器類主要提供了下面這些方法來使用它它們:1. 過濾;2. 遍歷;3. 分區; 4. 數學統計;5. 其餘。咱們來詳細看看這些方法
java

2. 容器通用方法(Common methods on Traversable collections)

  • c collect f:返回c中知足f函數條件的元素es6

scala> (1 to 10) collect { case x if x%2==0 => x*x }
res51: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 16, 36, 64, 100)
  • c count p:返回c中符合條件p的元素個數函數

scala> (1 to 10) count (_ > 8)
res53: Int = 2
  • c1 diff c2:返回c1中存在,c2中不存在的元素es5

scala> (1 to 5) diff (2 to 4)
res55: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 5)
  • c drop n:返回c中除前n個元素以外的全部元素spa

scala> (1 to 5) drop 2
res65: scala.collection.immutable.Range = Range(3, 4, 5)
  • c dropWhile p:從頭開始遍歷c,返回最長的知足條件p的全部元素集合scala

scala> (1 to 10) dropWhile (_ < 5)
res69: scala.collection.immutable.Range = Range(5, 6, 7, 8, 9, 10)
  • c exists p:c中任何一個元素知足p返回true,不然返回falsecode

scala> (1 to 10) exists (_ < 8)
res73: Boolean = true

scala> (1 to 10) exists (_ > 10)
res74: Boolean = false
  • c filter p:返回c中全部知足p的元素排序

scala> (1 to 10) filter (_ > 7)
res75: scala.collection.immutable.IndexedSeq[Int] = Vector(8, 9, 10)
  • c filterNot p:返回c中全部不知足p的元素索引

scala> (1 to 10) filterNot (_ > 7)
res76: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7)
  • c find p:返回c中第一個知足條件p的元素ip

scala> (1 to 10) find (_ > 7)
res77: Option[Int] = Some(8)
  • c flatten:把嵌套List展開爲單層List,好比List(List(1,2,3),List(4,5,6))轉爲List(1,2,3,4,5,6)

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

scala> l.flatten
res84: List[Int] = List(1, 2, 3, 4, 5, 6)
  • c flatMap f:先對容器c中的全部元素執行函數f,而後平鋪展開返回全部元素集合

scala> List(List(1,2,3),List(4,5,6)).flatMap(x => x.map(_ + 1))
res99: List[Int] = List(2, 3, 4, 5, 6, 7)
  • c foldLeft(z)(op):op是一個二元函數,從z開始與c中元素從左到右執行op操做,下面例子至關於2*1*2*3*4=48

scala> List(1,2,3,4).foldLeft(2)((x,y)=>x*y)
res116: Int = 48
  • c foldRight(z)(op):op一樣是一個二元函數,從z開始與c中元素從右到左執行op操做,例子:2*4*3*2*1=48

scala> List(1,2,3,4).foldRight(2)((x,y)=>x*y)
res117: Int = 48
  • c forall p:若是c中的每個元素都知足條件p,那麼返回true,不然返回flase

scala> List(1,2,3).forall(s => s < 4)
res130: Boolean = true

scala> List(1,2,3).forall(s => s < 3)
res131: Boolean = false
  • c foreach f:對容器c中的每個元素執行函數f

scala> List(1,2,3).foreach(print)
123
  • c groupBy f:用f函數對容器c的元素分組,返回一個Map類型數據,key爲分組名,value爲分組後的元素集合

scala> List(1,2,3,4).groupBy{x => x match{case x if x < 3 => 1;case x if x >=3 => 2}}
res137: scala.collection.immutable.Map[Int,List[Int]] = Map(2 -> List(3, 4), 1 -> List(1, 2))
  • c hasDefiniteSize:hasDefiniteSize。Traversable容器有有限和無限之分。比方說,天然數流Stream.from(0)就是一個無限的traversable 容器。hasDefiniteSize方法可以判斷一個容器是否多是無限的。若hasDefiniteSize返回值爲ture,容器確定有限。若返回值爲false,根據完整信息才能判斷容器(collection)是無限仍是有限

scala> Stream.from(0).hasDefiniteSize
res148: Boolean = false

scala> Vector(1,2,3,4).hasDefiniteSize
res149: Boolean = true
  • c head:返回容器c的第一個元素

scala> Vector(1,2,3,4).head
res150: Int = 1

scala> List(1,2,3,4).head
res151: Int = 1
  • c headOption:返回c的第一個元素Some(element),若是c爲空則返回None

scala> Vector(1,2,3).headOption
res155: Option[Int] = Some(1)

scala> Vector().headOption
res156: Option[Nothing] = None
  • c init:返回容器c除了最後一個元素以外的元素集合,若是c爲空跑出異常

scala> Vector(1,2,3).init
res157: scala.collection.immutable.Vector[Int] = Vector(1, 2)

scala> Vector().init
java.lang.UnsupportedOperationException: empty.init
  • c1 intersect c2:返回容器c1和c2的公共元素

scala> Vector(1,2,3,4) intersect Vector(2,3)
res161: scala.collection.immutable.Vector[Int] = Vector(2, 3)
  • c isEmpty:判斷容器是否爲空

scala> Vector(1,2,3,4).isEmpty
res162: Boolean = false

scala> Vector().isEmpty
res163: Boolean = true
  • c last:返回容器c的最後一個元素

scala> Vector(1,2,3).last
res164: Int = 3
  • c lastOption:返回容器c的最後一個元素Some(e),若是容器c爲空則返回None

scala> Vector(1,2,3).lastOption
res165: Option[Int] = Some(3)

scala> Vector().lastOption
res166: Option[Nothing] = None
  • c map f:對容器c中的每個元素執行函數f,並將f的返回值組成一個集合返回

scala> val v = Vector(1,2,3).map(println _)
1
2
3
v: scala.collection.immutable.Vector[Unit] = Vector((), (), ())

scala> Vector(1,2,3).map(_ + 1)
res170: scala.collection.immutable.Vector[Int] = Vector(2, 3, 4)
  • c min/max:返回容器c中最小/最大元素

scala> Vector(1,2,3).min
res171: Int = 1

scala> Vector(1,2,3).max
res172: Int = 3
  • c nonEmpty:判斷容器c是否不爲空,不爲空返回true,不然返回false

scala> Vector(1,2,3).nonEmpty
res173: Boolean = true

scala> Vector().nonEmpty
res174: Boolean = false
  • c par:返回集合c的並行實現

scala> Vector(1,2,3).par
res175: scala.collection.parallel.immutable.ParVector[Int] = ParVector(1, 2, 3)
  • c partition p:返回集合c按條件p分紅的兩個集合組成的二元祖

scala> Vector(1,2,3).partition(_ < 2)
res177: (scala.collection.immutable.Vector[Int], scala.collection.immutable.Vector[Int]) = (Vector(1),Vector(2, 3))
  • c product:返回容器c中全部元素的乘積

scala> Vector(1,1,3).product
res180: Int = 3
  • c reduceLeft op:容器c中元素從左到右執行二元函數op迭代,返回結果

scala> List(1,2,3).reduceLeft{(x,y)=>x+y}
res2: Int = 6
  • c reduceRight:容器c中元素從右到左執行二元函數op迭代,並返回計算結果

scala> List(1,2,3).reduceRight{(x,y)=>x+y}
res3: Int = 6
  • c reverse:生成集合c的反序集合

scala> List(1,2,3).reverse
res4: List[Int] = List(3, 2, 1)

scala> List().reverse
res5: List[Nothing] = List()
  • c size:返回集合c的長度

scala> List(1,2,3).size
res7: Int = 3
  • c slice(from,to):返回集合c中從from開始到to位置的元素集合,不包括to位置

scala> List(1,2,3,4,5).slice(0,3)
res11: List[Int] = List(1, 2, 3)
  • c sortwith f:返回集合c按f排序後的結果

scala> List(2,4,3,1,5).sortWith{(x,y)=>x>y}
res12: List[Int] = List(5, 4, 3, 2, 1)

scala> List(2,4,3,1,5).sortWith{(x,y)=>x<y}
res13: List[Int] = List(1, 2, 3, 4, 5)
  • c span p:把集合分爲兩部分,第一個集合是集合開始一直到第一個不知足p的元素以前,第二個集合是餘下的元素

scala> List(2,4,3,5,1).span(_<5)
res19: (List[Int], List[Int]) = (List(2, 4, 3),List(5, 1))
  • c splitAt n:把集合分爲兩部分,第一部分是從開始一直到第一個連續等於n的元素,餘下爲第二部分

scala> List(2,4,3,5,1).splitAt(3)
res20: (List[Int], List[Int]) = (List(2, 4, 3),List(5, 1))

scala> List(2,4,3,5,1).splitAt(10)
res21: (List[Int], List[Int]) = (List(2, 4, 3, 5, 1),List())

scala> List(2,3,3,5,1).splitAt(3)
res22: (List[Int], List[Int]) = (List(2, 3, 3),List(5, 1))

scala> List(2,3,4,5,3).splitAt(3)
res23: (List[Int], List[Int]) = (List(2, 3, 4),List(5, 3))
  • c sum:對集合c中的元素求和,返回求和結果

scala> List(1,2,3).sum
res24: Int = 6
  • c tail:返回集合c中除了頭元素以外的其餘元素

scala> List("1","2","3").tail
res2: List[String] = List(2, 3)
  • c take n:返回結合c中前n個元素組成的集合

scala> List(1,2,3) take 2
res3: List[Int] = List(1, 2)
  • c takeWhile p:返回集合c中連續知足p的元素集合

scala> List(1,2,3,3,4,5).takeWhile(_<=3)
res4: List[Int] = List(1, 2, 3, 3)

scala> List(1,2,3,4,5,3).takeWhile(_<=3)
res5: List[Int] = List(1, 2, 3)
  • c1 union c2:返回集合c1和集合c2的並集

scala> List(1,2,3) union List(2,3,4)
res6: List[Int] = List(1, 2, 3, 2, 3, 4)
  • c1 zip c2:返回c1和c2元素對應的二元組組成的集合

  • c unzip:對zip操做後的集合進行反zip操做

scala> val l1 = List(1,2,3)
l1: List[Int] = List(1, 2, 3)

scala> val l2 = List("a","b","c")
l2: List[String] = List(a, b, c)

scala> l1 zip l2
res9: List[(Int, String)] = List((1,a), (2,b), (3,c))

scala> val l3 = l1 zip l2
l3: List[(Int, String)] = List((1,a), (2,b), (3,c))

scala> l3.unzip
res10: (List[Int], List[String]) = (List(1, 2, 3),List(a, b, c))
  • c view:生成結合c的懶加載試圖

scala> List(1,2,3).view
res8: scala.collection.SeqView[Int,List[Int]] = SeqView(...)
  • c zipWithIndex:返回c的元素與其索引組成的二元組集合

scala> List("a","b","c").zipWithIndex
res14: List[(String, Int)] = List((a,0), (b,1), (c,2))
相關文章
相關標籤/搜索