您的位置:首页 > 其它

scala 实现WordCount

2016-08-16 18:00 363 查看
object WC {

def main(args: Array[String]): Unit = {

val list = Source.fromFile("E:/words.txt").getLines().toList

.map((_,1))

.groupBy(_._1)

.mapValues(_.map(_._2).reduce(_+_))

list.foreach(println(_))

val l = List(1,2,3,4,5,6,7,8,9)

val sum = l.reduce{_+_}

println("sum=>"+sum)

}

def WC(): Unit = {

val list = Source.fromFile("E:/words.txt").getLines().toList

.map { x => (x,1) }

.groupBy(x=>x._1)

.mapValues{

list=>list.map(i=>i._2).reduce((x,y)=>x+y)

}

list.foreach(x=>println(x))

}

def WC2(): Unit = {

val list = Source.fromFile("E:/words.txt").getLines().toList

.map { x => (x,1) }

.groupBy(x=>x._1)

.mapValues(list=>list.map(i=>i._2))

.mapValues(l => l.reduce((x,y)=>x+y))

list.foreach(x=>println(x))

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: