IntelliJ IDEA上scala插件安装、wordCount词频分析案例(可读性高)

Scala插件安装

configure –>pluin –>install JetBrains plugin—>搜索Scala
这里写图片描述
这里写图片描述
这里写图片描述
选中scala后,点击右侧窗口的install,等待安装,安装完成后restart,重启IntelliJ IDEA

wordCount词频分析案例

1、新建项目create new project
这里写图片描述
这里写图片描述
2、新建scala类
这里写图片描述
建类时一定要选择object类型
这里写图片描述
3、编写代码

object WordCount {
  def main(args: Array[String]): Unit = {
    //定义数组
    val lines = Array("hello mm hello hh","hello am hello ah","mm hh am mm")
    //切分压平
    val words: Array[String] = lines.flatMap(line => line.split(" "))
    //写成 hello 1格式
    val wordAndone: Array[(String, Int)] = words.map(word => (word,1))
    //按单词分组
    val gruoped: Map[String, Array[(String, Int)]] = wordAndone.groupBy(t => t._1)
    //计算词频
    val count: Map[String, Int] = gruoped.map(t => (t._1,t._2.length))
    //降序排列
    val result: List[(String, Int)] = count.toList.sortBy(t => t._2).reverse
    println(result)
  }
}

4、运行
右键,选择run
结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/amin_hui/article/details/82531177