首页 > 其他分享 >HashMap实战---实现词频统计

HashMap实战---实现词频统计

时间:2022-12-08 00:55:06浏览次数:44  
标签:Map HashMap value --- 词频 key words String

实现原理:

  用HashMap存储该单词以及单词出现的次数。遍历每一个单词,如果单词在HashMap中的键存在,则将该键值对对应的值+1;

  如果单词在HashMap中的键不存在,则将该键存储到HashMap中,对应的值为1。

代码实现:

package HashMap;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class HashMapDemo {
    @SuppressWarnings("preview")
    public static void main(String[] args){
        Map<String,Integer> wordCountStore = new HashMap<>();

        String wordString = """
                Give me the strength lightly to bear my jobs and sorrows"
                Give me the strength to make my love fruitful in service
                Give me the strength never to disown the poor or bend my kneed
                before insolent migh
                Give me the strength to raise my mind high above daily trifles
                """;

        String[] words = wordString.replace("\n"," ").strip().split(" ");
     
     for (String w:words){
    System.out.print(w + " ");
      }

for(String word:words){ String key = word.toLowerCase(); Integer value = wordCountStore.get(key); if(value == null){ value = 0; } value++; wordCountStore.put(key,value); } for(Map.Entry<String,Integer> entry: wordCountStore.entrySet()){ String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key + ":" + value); } } }

注:上述wordString这个文本段落需要JDK13版本以上才能支持,本实例所选版本为jdk17,首先将该文本转化为文本数组words,遍历words,每个单词转为小写后作为Map的key,如果

Map中该key对应的value,则值累加1,否则先将value置为0,在累加1,最后value的结果存储到Map中。

运行结果如下:

 

标签:Map,HashMap,value,---,词频,key,words,String
From: https://www.cnblogs.com/99kol/p/16965018.html

相关文章

  • ShardingCore兼容Code-First数据库迁移 让update-database能更新分表
    参考文章.Net下极限生产力之分表分库全自动化MigrationsCode-First背景在我上一篇博客ABPEFCORE7集成ShardingCore实现分表写完之后,我发现我还有一个没解决的问......
  • 爬取的数据存入csv文件-用excel直接打开乱码解决方法
    如图所示:解决方法:用记事本打开另存为,编码格式为ansicsv部分源码......
  • Start-Start
    感觉卷就能变diao,但是卷不动。——说明不能变diao。那怎么变diao呢?——不能。今天发现AirPods可以吸在Macbook上。但是自从发现了,我就一直吸着,再也没用过AirPods......
  • C++学习---cstdio的源码学习分析01-类型定义
    引言cstdio文件是C++对stdio.h头文件的封装,StandardInputandOutputLibrary,定义了一系列标准输入输出函数,包括文件操作(fopen/fclose等),格式化打印(printf/scanf)等。通......
  • 【Linux】循序渐进学运维-服务篇-LNMP环境部署
    文章目录前言一.LNMP的介绍二.什么是Fast-CGI三.部署实战安装php和mysqlnginx和php的结合a.编译nginxb.配置nginx与php的结合c.重启nginx让配置生效d.配置index......
  • 学成在线项目-精品推荐模块
    学成在线项目-精品推荐模块思路:大盒子包含三个小盒子,左边精品推荐,中间用小li包含小a,右边是修改盒子。分析完毕,开干。1、效果图如下2、html代码如下:<!DOCTYPEhtml......
  • 嵌入式-链表尾插法插入
    链表通过尾部插入#include<stdio.h>#include<stdlib.h>structTest{intdata;structTest*next;};printfLink(structTest*head){while(head!=NUL......
  • 软件技术基础(4)-小组介绍
    这个作业属于哪个课程软件技术基础第四次作业这个作业的目标成立小组,讨论并确定大作业的题目及分工小组成员严隽铭-2019330301032陈奕涛-2019330301007......
  • CF799E Aquarium Decoration - 贪心 - 堆 -
    题目链接:https://codeforces.com/contest/799/problem/E题解:考虑枚举都喜欢的个数\(s\),那么只有A喜欢的有\(k-s\)个,B喜欢的\(k-s\)个然后我只需要找所有的\(x\)......
  • ArcGIS插件-太乐地图
    在很多行业,打败你的往往不是竞争对手,而是你意想不到的其他因素。回想十年前,如果能预料到现在的处境,也许当时的心态和做法会有所不同。在多年前,市场上出现包括水经注、太乐......