Scala入门学习之【wordcount】
2017-11-02 17:12
330 查看
[plain] view
plain copy
val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")
println(lines.flatMap(_.split(" ")))
//List(hello, tom, hello, jerry, hello, jerry, hello, kitty)
//flatMap中的_表示一行内容 hello tom hello jerry 一共有三行 即对每行操作
//flatMap(_.split(" ")) 中的_.split(" ") 就相当于"hello tom hello jerry".split(" ")
println(lines.flatMap(_.split(" ")).map((_ ,1)))
//List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (jerry,1), (hello,1), (kitty,1))
//返回的List集合,集合里边每一个元素为元组 访问元组的第一个元素为_._1
//例如:println((""hello",1)._1) 结果为hello
//lines.flatMap(_.split(" ")).map((_ ,1))中的map((_ ,1) _表示每一个单词,1表示每出现一次计数为1
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1))
//Map(tom -> List((tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1), (jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1)))
// lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1)中的groupBy(_._1)表示按照list中每个元组中的第一个字段分组即拿第一个字段作为key,返回结果是一个大Map
//groupBy(_._1)中的第一个_表示list中的每一个元组,而 ._1 表示取每一个元组中的第一个元素
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)))
//Map(tom -> 1, kitty -> 1, jerry -> 2, hello -> 4)
// lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues()中的mapValues()仅仅会对value处理,处理完了把key 结合起来
// mapValues()中的第一个_表示map里边的value ,而value是一个list
//lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)) 中的foldLeft(0)是给一个初始值
// (_+_._2)中的第一个_表示初始值或者累加过的值,第二个_表示List里边的元组,._2表示拿到元组中的第二个字段
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList)
// List((tom,1), (kitty,1), (jerry,2), (hello,4))
// 转化为List
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2))
//List((tom,1), (kitty,1), (jerry,2), (hello,4))
//sortBy(_._2)中的第一个_ 表示每一个元组,第二个._2 每个元组中的第二个字段
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2).reverse)
//List((hello,4), (jerry,2), (kitty,1), (tom,1))
//reverse表示降序排序
plain copy
val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")
println(lines.flatMap(_.split(" ")))
//List(hello, tom, hello, jerry, hello, jerry, hello, kitty)
//flatMap中的_表示一行内容 hello tom hello jerry 一共有三行 即对每行操作
//flatMap(_.split(" ")) 中的_.split(" ") 就相当于"hello tom hello jerry".split(" ")
println(lines.flatMap(_.split(" ")).map((_ ,1)))
//List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (jerry,1), (hello,1), (kitty,1))
//返回的List集合,集合里边每一个元素为元组 访问元组的第一个元素为_._1
//例如:println((""hello",1)._1) 结果为hello
//lines.flatMap(_.split(" ")).map((_ ,1))中的map((_ ,1) _表示每一个单词,1表示每出现一次计数为1
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1))
//Map(tom -> List((tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1), (jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1)))
// lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1)中的groupBy(_._1)表示按照list中每个元组中的第一个字段分组即拿第一个字段作为key,返回结果是一个大Map
//groupBy(_._1)中的第一个_表示list中的每一个元组,而 ._1 表示取每一个元组中的第一个元素
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)))
//Map(tom -> 1, kitty -> 1, jerry -> 2, hello -> 4)
// lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues()中的mapValues()仅仅会对value处理,处理完了把key 结合起来
// mapValues()中的第一个_表示map里边的value ,而value是一个list
//lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)) 中的foldLeft(0)是给一个初始值
// (_+_._2)中的第一个_表示初始值或者累加过的值,第二个_表示List里边的元组,._2表示拿到元组中的第二个字段
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList)
// List((tom,1), (kitty,1), (jerry,2), (hello,4))
// 转化为List
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2))
//List((tom,1), (kitty,1), (jerry,2), (hello,4))
//sortBy(_._2)中的第一个_ 表示每一个元组,第二个._2 每个元组中的第二个字段
println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2).reverse)
//List((hello,4), (jerry,2), (kitty,1), (tom,1))
//reverse表示降序排序
相关文章推荐
- Scala入门学习之【wordcount】
- Scala 入门 WordCount
- hadoop入门之wordcount学习
- 知识学习——Hadoop MapReduce开发入门程序WordCount详解
- Scala 学习(六)--- 单机实现wordcount详细解读
- Flink学习笔记 --- scala实现Flink的DataSet Source进行WordCount
- Spark2.x学习笔记:16、Spark Streaming入门实例NetworkWordCount
- Spark Streaming开发入门——WordCount(Java&Scala)
- hadoop入门学习系列之二hadoop的mapreduce的wordcount流程介绍
- hadoop入门学习系列之五Eclipse下搭建Hadoop2.6.5开发环境并写wordcount
- Scala入门学习笔记四--List使用
- scala学习之协变逆变入门(一)
- Spark入门的WordCount
- Spark学习笔记@第一个例子wordcount+Eclipse
- scala版本wordcount的几种写法
- windows中用scala-IDE开发spark—— WordCount
- Spark:用Scala和Java实现WordCount
- MapReduce入门级之WordCount单词计数
- WordCount案例---MapReduce学习小结(-)
- scala-eclipse 编写spark简单程序 WordCount