scala函数式编程初体验 ==> 用scala函数实现 WordCount
//定义一个字符串集合
scala> val lines = List("hadoop,hive,spark,hue,mapreduce"," ","hadoop,hue","hdfs, , ,hive","","hadoop,spark")
lines: List[String] = List(hadoop,hive,spark,hue,mapreduce, " ", hadoop,hue, hdfs, , ,hive, "", hadoop,spark)
//功能1 :(不算空格和空串)统计一下集合中总共有多少个单词?
scala> lines.flatMap(_.split(",")).filter(_.trim.nonEmpty).map((_,1)).map(_._2).reduce(_+_)
res164: Int = 11
//功能2 : 分别统计每一个单词出现的次数
scala> lines.flatMap(_.split(",")).filter(_.trim.nonEmpty).groupBy(_.toString).mapValues(_.length).foreach(println)
(mapreduce,1)
(hue,2)
(hadoop,3)
(spark,2)
(hive,2)
(hdfs,1)