scala高阶函数--groupby/sortby/patition

 

 

package day3

object demo_high_func {
    def main(args: Array[String]): Unit = {
        val list = Array(1,2,3,4,5,6,7,7,7,2,8)
        // 过滤数组中的元素, 将为true的元素组合成一个新的数组
        val list2 = list.filter(e=>e%2==0)
        println(list2.mkString("[",",","]"))

        // map函数,对每个元素操作
        println("-" * 40 + "map" + "-" * 40)
        val list3 = list.map(n=>n * 10)
        println(list3.mkString("[",",","]"))

        println("-" * 40 + "flatMap" + "-" * 40)
        val words = Array("hello you", "hello me", "hello other")
        val flist = words.flatMap(word=>word.split(" "))
        println(flist.mkString("[",",","]"))

        println("-" * 40 + "reduce" + "-" * 40)
        //聚合函数, 啥意思看打印结果吧
        val relist = list.reduce((a1,a2)=>{
            println("v1= " + a1 + ",v2=" +a2)
            a1 + a2
        })
        // 普通写法
        val res = list.reduce((v1,v2)=>v1+v2)
        //简单写法
        val res1 =  list.reduce(_ + _)
        println(res)
        println(res1)

        println("-" * 40 + "drowhile" + "-" * 40)
        val droplist = list.dropWhile(n=>n!=3)
        println(droplist.mkString("[",",","]"))

        println("-" * 40 + "sortwith" + "-" * 40)
        val newlist = Array(1,3,8,4,-1,7,6,10)
        val sortlist = newlist.sortWith((n1,n2)=>n1>n2) // 从大到小排序
        val sortlist2 = newlist.sortWith(_ > _)  // 简写
        println(newlist.mkString("[",",","]"))
        println(sortlist.mkString("[",",","]"))
        println(sortlist2.mkString("[",",","]"))

        println("-" * 40 + "groupby" + "-" * 40)
        // 相当于sql中的group by , 返回值为不可变的map类型
        val wordmap:Map[String,Array[String]] = flist.groupBy(word => word)
//        println(wordmap.mkString("[",",","]"))
        wordmap.foreach(kv=>println(kv._1,kv._2.mkString("[",",","]")))

        println("-" * 40 + "partition" + "-" * 40)
        // 将一个集合分成两个, 能被2整除的放一个集合, 返回两个集合
        val(left, right) = newlist.partition(n => n%2==0)
        println(left.mkString("[",",","]"))
        println(right.mkString("[",",","]"))
    }

}
package day3

object demo_high_func {
    def main(args: Array[String]): Unit = {
        val list = Array(1,2,3,4,5,6,7,7,7,2,8)
        // 过滤数组中的元素, 将为true的元素组合成一个新的数组
        val list2 = list.filter(e=>e%2==0)
        println(list2.mkString("[",",","]"))

        // map函数,对每个元素操作
        println("-" * 40 + "map" + "-" * 40)
        val list3 = list.map(n=>n * 10)
        println(list3.mkString("[",",","]"))

        println("-" * 40 + "flatMap" + "-" * 40)
        val words = Array("hello you", "hello me", "hello other")
        val flist = words.flatMap(word=>word.split(" "))
        println(flist.mkString("[",",","]"))

        println("-" * 40 + "reduce" + "-" * 40)
        //聚合函数, 啥意思看打印结果吧
        val relist = list.reduce((a1,a2)=>{
            println("v1= " + a1 + ",v2=" +a2)
            a1 + a2
        })
        // 普通写法
        val res = list.reduce((v1,v2)=>v1+v2)
        //简单写法
        val res1 =  list.reduce(_ + _)
        println(res)
        println(res1)

        println("-" * 40 + "drowhile" + "-" * 40)
        val droplist = list.dropWhile(n=>n!=3)
        println(droplist.mkString("[",",","]"))

        println("-" * 40 + "sortwith" + "-" * 40)
        val newlist = Array(1,3,8,4,-1,7,6,10)
        val sortlist = newlist.sortWith((n1,n2)=>n1>n2) // 从大到小排序
        val sortlist2 = newlist.sortWith(_ > _)  // 简写
        println(newlist.mkString("[",",","]"))
        println(sortlist.mkString("[",",","]"))
        println(sortlist2.mkString("[",",","]"))

        println("-" * 40 + "groupby" + "-" * 40)
        // 相当于sql中的group by , 返回值为不可变的map类型
        val wordmap:Map[String,Array[String]] = flist.groupBy(word => word)
//        println(wordmap.mkString("[",",","]"))
        wordmap.foreach(kv=>println(kv._1,kv._2.mkString("[",",","]")))

        println("-" * 40 + "partition" + "-" * 40)
        // 将一个集合分成两个, 能被2整除的放一个集合, 返回两个集合
        val(left, right) = newlist.partition(n => n%2==0)
        println(left.mkString("[",",","]"))
        println(right.mkString("[",",","]"))
    }

}

 

posted on 2021-08-19 07:08  孔扎根  阅读(506)  评论(0编辑  收藏  举报

导航