1. 综述
Scala的容器类主要提供了下面这些方法来使用它它们:1. 过滤;2. 遍历;3. 分区; 4. 数学统计;5. 其他。我们来详细看看这些方法
2. 容器通用方法(Common methods on Traversable collections)
c collect f:返回c中满足f函数条件的元素
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中不存在的元素
scala> (1 to 5) diff (2 to 4)
res55: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 5)
c drop n:返回c中除前n个元素之外的所有元素
scala> (1 to 5) drop 2
res65: scala.collection.immutable.Range = Range(3, 4, 5)
c dropWhile p:从头开始遍历c,返回最长的满足条件p的所有元素集合
scala> (1 to 10) dropWhile (_ < 5)
res69: scala.collection.immutable.Range = Range(5, 6, 7, 8, 9, 10)
c exists p:c中任何一个元素满足p返回true,否则返回false
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的元素
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))
来源:oschina
链接:https://my.oschina.net/u/2633112/blog/652155