package cn.allengao.exercise object ScalaWordCount { def main(args: Array[String]): Unit = { val lines = List("hello java hello python","hello scala","hello scala hello java hello scala") //切分并压平,用空格来切分单词,并放到map中 val words = lines.flatMap(_.split(" ")) //把每个单词生成一个一个的tuple val tuples = words.map((_,1)) //以Key(单词)进行分组,_拿到元祖,._1表示key,._2表示value,分成三个大的List val grouped = tuples.groupBy(_._1) //统计List中value的长度 val sumed = grouped.mapValues(_.size) //对结果进行排序,_表示(key,value),._2 表示value val sorted = sumed.toList.sortBy(_._2) //数量从高往低降序排列 val result = sorted.reverse println(words) println(tuples) println(grouped) println(sumed) println(sorted) println(result) val result1 = lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size).toList.sortBy(_._2).reverse val result2 = lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2).reverse val result3 = lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t=>(t._1, t._2.size)).toList.sortBy(_._2).reverse println("1 "+ result1) println("2 "+ result2) println("3 "+ result3) } }