def main(args: Array[String]): Unit = { // val source = Source.fromFile("input/input02.txt") // val list = source.getLines().toList.dropRight(1) // source.close() val list = List( ("zhangsan", "河北", "鞋"), ("lisi", "河北", "衣服"), ("wangwu", "河北", "鞋"), ("zhangsan", "河南", "鞋"), ("lisi", "河南", "衣服"), ("wangwu", "河南", "鞋"), ("zhangsan", "河南", "鞋"), ("lisi", "河北", "衣服"), ("wangwu", "河北", "鞋"), ("zhangsan", "河北", "鞋"), ("lisi", "河北", "衣服"), ("wangwu", "河北", "帽子"), ("zhangsan", "河南", "鞋"), ("lisi", "河南", "衣服"), ("wangwu", "河南", "帽子"), ("zhangsan", "河南", "鞋"), ("lisi", "河北", "衣服"), ("wangwu", "河北", "帽子"), ("lisi", "河北", "衣服"), ("wangwu", "河北", "电脑"), ("zhangsan", "河南", "鞋"), ("lisi", "河南", "衣服"), ("wangwu", "河南", "电脑"), ("zhangsan", "河南", "电脑"), ("lisi", "河北", "衣服"), ("wangwu", "河北", "帽子") ) println(list) //println(list.flatMap(_.split(", "))) println(list.map(tuple => { (tuple._1,tuple._2 + "" + tuple._3) })) println(list.map(tuple => { (tuple._1, tuple._2 + "" + tuple._3) }).groupBy(str => { str._2 })) println(list.map(tuple => { (tuple._1, tuple._2 + "" + tuple._3) }).groupBy(str => { str._2 }).map(kv => { (kv._1, kv._2.length) })) println(list.map(tuple => { (tuple._1, tuple._2 + "" + tuple._3) }).groupBy(str => { str._2 }).map(kv => { (kv._1, kv._2.length) }).toList.sortBy(str => { str._2 })(Ordering.Int.reverse)) val result: Map[String, List[(String, Int)]] = list.map(t => { (t._2, t._3)//去除用户名信息,结构转化,使用map,("河北,鞋子") }) .groupBy(_._1)//按照省份进行分组,("河北",("河北,鞋子")) .map(t => { val list: List[(String, Int)] = t._2.map(_._2) //将("河北,鞋子") -> ("鞋子") .groupBy(pro => pro) .map(tuple => { (tuple._1, tuple._2.length) }) .toList .sortBy(_._2)(Ordering.Int.reverse) (t._1, list) }) println(result) }
Scala中常见wordcount案例(二)
最新推荐文章于 2023-06-29 19:43:15 发布