Scala集合常用算子

排序

  • sorted
def sorted[B >: Int](implicit ord: scala.math.Ordering[B]): Array[Int]
scala> var array=Array(1,2,4,5,3)
array: Array[Int] = Array(1, 2, 4, 5, 3)
scala> list.sorted
res4: List[Int] = List(1, 2, 3, 4, 5)
  • sortBy
def sortBy[B](f: (String,Int) => B)(implicit ord: scala.math.Ordering[B]): List[(String,Int)]
scala> var list=Array(("a",1),("b",2),("d",5),("c",4))
list: Array[(String, Int)] = Array((a,1), (b,2), (d,5), (c,4))

scala> list.sortBy(t=>t._2) // list.sortBy(_._2)
res7: Array[(String, Int)] = Array((a,1), (b,2), (c,4), (d,5))

scala> list.sortBy(t=>t._1) //list.sortBy(_._2)
res8: Array[(String, Int)] = Array((a,1), (b,2), (c,4), (d,5))
  • sortWith
    可以指定多个Field做联合排序
def sortWith(lt: ((String, Int), (String, Int)) => Boolean): Array[(String, Int)]
scala> var list=Array(("a",2),("a",1),("b",2),("d",5),("c",4))
list: Array[(String, Int)] = Array((a,2), (a,1), (b,2), (d,5), (c,4))

scala>     val tuples = list.sortWith((t1, t2) => {
            if (!t1._1.equals(t2._1)) {
              t1._1.compareTo(t2._1) < 0
            } else {
              t1._2.compareTo(t2._2) < 0
            }
          })
tuples: Array[(String, Int)] = Array((a,1), (a,2), (b,2), (c,4), (d,5))

flatten(拉平)

scala> var list=Array(Array(1,2,3),Array(4,5))
scala> list.flatten
res6: Array[Int] = Array(1, 2, 3, 4, 5)

map(转换)

scala> var list=Array("this is a demo","hello word")
list: Array[String] = Array(this is a demo, hello word)
scala> list.map(line=>line.split(" ")) // 等价list.map(_.split(" "))
res13: Array[Array[String]] = Array(Array(this, is, a, demo), Array(hello, word))

flatMap

scala>  var list=Array("this is a demo","hello word")
list: Array[String] = Array(this is a demo, hello word)
scala> list.flatMap(_.split(" "))
res16: Array[String] = Array(this, is, a, demo, hello, word)

scala> list.flatMap(_.split(" ")).map((_,1))
res17: Array[(String, Int)] = Array((this,1), (is,1), (a,1), (demo,1), (hello,1)
, (word,1))

scala> list.flatMap(line=> (for(i<-line.split(" ")) yield (i,1)))
res20: Array[(String, Int)] = Array((this,1), (is,1), (a,1), (demo,1), (hello,1)
, (word,1))

filter/filterNot

scala>  var list=Array("this is a demo","hello word")
list: Array[String] = Array(this is a demo, hello word)

scala> list.filter(line=>line.contains("hello"))
res24: Array[String] = Array(hello word)

scala> list.filterNot(line=>line.contains("hello"))
res25: Array[String] = Array(this is a demo)

groupBy

scala> var list=Array("good","good","study")
list: Array[String] = Array(good, good, study)

scala> list.groupBy(item=>item)
res30: scala.collection.immutable.Map[String,Array[String]] = Map(study -> Array
(study), good -> Array(good, good))

scala> list.groupBy(item=>item).map(t=>(t._1,t._2.size))
res34: scala.collection.immutable.Map[String,Int] = Map(study -> 1, good -> 2)

fold

scala> var lst=Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
lst: Array[Int] = Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)

scala>     lst.fold[Int](0)((v1,v2)=>v1+v2) // lst.fold(0)(_+_)
res52: Int = 44

aggregate

scala> var lst=Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
lst: Array[Int] = Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)

scala> lst.aggregate
def aggregate[B](z: => B)(seqop: (B, Int) => B,combop: (B, B) => B): B

scala> lst.aggregate(0)((v1,v2)=>v1+v2,(b1,b2)=>b1+b2) //lst.aggregate(0)(_+_,_+_)
res53: Int = 44

reduce

scala> var lst=Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
lst: Array[Int] = Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)

scala> lst.reduce
   def reduce[A1 >: Int](op: (A1, A1) => A1): A1

scala> lst.reduce((v1,v2)=>v1+v2)//lst.reduce(_+_)
res54: Int = 44

字符统计

var arrs=Array("this is a demo","good good study","day day up")
arrs.flatMap(_.split(" "))
.groupBy(word=>word)
.toList
.map(tuple=>(tuple._1,tuple._2.size))
.sortBy(_._1)
.foreach(println)
  • 元组计算
var arrs=Array("this is a demo","good good study","day day up")
arrs.flatMap(_.split(" "))
.map((_, 1))
.groupBy(_._1)
.toList
.map(t=>(t._1,t._2.map(_._2).sum))
.sortBy(_._2)
.foreach(println)

猜你喜欢

转载自blog.csdn.net/weixin_43655644/article/details/93887601
今日推荐