给 Traversable对象的元素分组
根据条件将一个 Traversable 对象进行分组
解决方案:
1、splitAt
2、span
3、partition
4、groupBy
splitAt
package main object Test { def main(args: Array[String]): Unit = { val t1 = Traversable(1 to 10: _*); println(t1); //List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val result = t1.splitAt(3); //def splitAt(n : Int) : Tuple2[Repr, Repr] println(result.getClass.getName); //scala.Tuple2 println(result); //(List(1, 2, 3),List(4, 5, 6, 7, 8, 9, 10)) println(result._1.getClass.getName); //scala.collection.immutable.$colon$colon println(result._1); //List(1, 2, 3) println(result._2); //List(4, 5, 6, 7, 8, 9, 10) } }
splitAt 根据位置将 Traversable 对象分成两部分。
功能类似( x.take(n)、x.drop(n) ),但是更加有效 。
span
package main object Test { def main(args: Array[String]): Unit = { val t1 = Traversable(1 to 10: _*); println(t1); //List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val result = t1.span(x => x > 4); //def span(p : Function1[A, Boolean]) : Tuple2[Repr, Repr] println(result.getClass.getName); //scala.Tuple2 println(result); //(List(),List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) println(result._1.getClass.getName); //scala.collection.immutable.Nil$ println(result._1); //List() println(result._2); //List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val span = Traversable(1, 3, 5, 7, 9, 2, 4, 6, 8, 10).span(_ < 2); println(span.getClass.getName); //scala.Tuple2 println(span); //(List(1),List(3, 5, 7, 9, 2, 4, 6, 8, 10)) println(span._1.getClass.getName); //scala.collection.immutable.$colon$colon println(span._1); //List(1) println(span._2); //List(3, 5, 7, 9, 2, 4, 6, 8, 10) } }
span也是将 Traversable对象分成两部分,它会一直选取元素,直到某个元素不满足断言,
然后将前面的元素分成一组,将后面的元素分成另一组,共两部分。
功能类似( x.taskWhile(n)、x.dropWhile(n) ),但是更加有效。
partition
package main object Test { def main(args: Array[String]): Unit = { val t1 = Traversable(1 to 10: _*); println(t1); //List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val result = t1.partition(x => x % 2 == 0); //def partition(p : Function1[A, Boolean]) : Tuple2[Repr, Repr] println(result.getClass.getName); //scala.Tuple2 println(result); //(List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9)) println(result._2.getClass.getName); //scala.collection.immutable.$colon$colon println(result._1); //List(2, 4, 6, 8, 10) println(result._2); //List(1, 3, 5, 7, 9) val partition = Traversable(1, 3, 5, 7, 9, 2, 4, 6, 8, 10).partition(_ > 5); println(partition.getClass.getName); //scala.Tuple2 println(partition); //(List(7, 9, 6, 8, 10),List(1, 3, 5, 2, 4)) println(partition._1.getClass.getName); //scala.collection.immutable.$colon$colon println(partition._1); //List(7, 9, 6, 8, 10) println(partition._2); //List(1, 3, 5, 2, 4) } }
和 span 不同的是,span 遇到某个元素不满足断言的时候就不再使用断言进行分割。
partition 是会对所有的元素进行断言,断言结果为true的分为一组,等于fasle的分为另一组。
partition 是将满足断言的元素和不满足断言的元素分成两部分。
第一部分是满足断言的元素的集合。第二部分是不满足断言元素的集合。
groupBy
package main object Test { def main(args: Array[String]): Unit = { val t1 = Traversable(1 to 10: _*); println(t1); //List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val result = t1.groupBy(x => x % 3); //def groupBy[K](f : Function1[A, K]) : immutable.Map[K, Repr] println(result.getClass.getName); //scala.collection.immutable.Map$Map3 println(result); //Map(2 -> List(2, 5, 8), 1 -> List(1, 4, 7, 10), 0 -> List(3, 6, 9)) println(result.toList); //List((2,List(2, 5, 8)), (1,List(1, 4, 7, 10)), (0,List(3, 6, 9))) println(result.size); //3 println(result.keys); //List(2, 1, 0) val groupBy = Traversable(1, 3, 5, 7, 9, 2, 4, 6, 8, 10).groupBy(_ > 4); println(groupBy.getClass.getName); //scala.collection.immutable.Map$Map2 println(groupBy); //Map(false -> List(1, 3, 2, 4), true -> List(5, 7, 9, 6, 8, 10)) println(groupBy.toList.getClass.getName); //scala.collection.immutable.$colon$colon println(groupBy.toList); //List((false,List(1, 3, 2, 4)), (true,List(5, 7, 9, 6, 8, 10))) println(groupBy.size); //2 println(groupBy.keys); //List(false, true) } }
println(result.size); //3 println(result.keys); //List(2, 1, 0) println(result.values); //MapLike.DefaultValuesIterable(List(2, 5, 8), List(1, 4, 7, 10), List(3, 6, 9)) println(groupBy.size); //2 println(groupBy.keys); //List(false, true) println(groupBy.values); //MapLike.DefaultValuesIterable(List(1, 3, 2, 4), List(5, 7, 9, 6, 8, 10))
groupBy是按照条件将元素分组的,它根据提供的函数生成分组关键字。

浙公网安备 33010602011771号