Source.fromFile("input\\input.txt").
getLines().toList.
flatMap(_.split(" ")).
groupBy(kv => kv).map(kv => (kv._1, kv._2.length)).
toList.sortBy(kv => kv._2)(Ordering.Int.reverse).
foreach(kv => print(kv._1 + "->" + kv._2 + " "))
缺点:Source的资源关不了
最好这样分开写
val source: BufferedSource = Source.fromFile("input\\input.txt")
val datalist: List[String] = source.getLines().toList
source.close() //关闭资源
datalist.
flatMap(_.split(" ")).
groupBy(kv => kv).map(kv => (kv._1, kv._2.length)).
toList.sortBy(kv => kv._2)(Ordering.Int.reverse).
foreach(kv => print(kv._1 + "->" + kv._2 + " "))