请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

wordcount实现

	val txt = scala.io.Source.fromFile("/path/to/wcfile").mkString
	val txts = List(txt)

  val wcMap = txts.flatMap(_.split(","))
    .map(x => (x, 1))
    .groupBy(_._1)

  for((word, wordList) <- wcMap) {
    println(s"$word:${wordList.length}")
  }

正在回答 回答被采纳积分+3

1回答

Michael_PK 2020-02-07 03:15:27

想想如果不用for呢

0 回复 有任何疑惑可以回复我~
  • 提问者 chang_bryant #1
    想了个不用for的
    val txt = scala.io.Source.fromFile(
        "/path/to/wcfile").mkString
      val txts = List(txt)
      txts.flatMap(_.split(","))
        .map(x => (x, 1))
        .groupBy(_._1)
        .mapValues(_.length)
        .foreach(x => println(x._1 + ":" + x._2))
    回复 有任何疑惑可以回复我~ 2020-02-07 12:11:04
  • Michael_PK 回复 提问者 chang_bryant #2
    嗯啦!
    回复 有任何疑惑可以回复我~ 2020-02-07 12:46:57

相似问题

登录后可查看更多问答,登录/注册

问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号