• java统计出现频率最高的3个单词,并输出出现次数


    有如下字符串"In school and life, the most important driving force of work is the pleasure in work, the pleasure of working as a result, and the recognition of the social value of this result. "(用空格间隔)
    编码实现,统计出现频率最高的3个单词,并输出出现次数
    要求:
    打印格式:
    the=5
    of=4
    and=2
    (对于出现重复次数一样的,输出其中一个即可)

    package com.sinosoft.system;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    /**
     * @author WangTingWei
     * @Date 2022/11/9 19:09
     * @description
     */
    public class Meituan {
        public static void main(String[] args) throws Exception {
            // 题目提供的英文句子
            String s = "In school and life, the most important driving force of work is the pleasure in work, the pleasure of working as a result, and the recognition of the social value of this result.";
            // 通过切割方法获取String数组从而方便统计每个单词出现数量
            String[] wordList = s.split(" ");
            // 通过map键值存储单词以及出现数量
            HashMap<String, Integer> map = new HashMap<>(16);
            // 遍历单词数组
            for (int i = 0; i < wordList.length; i++) {
                // 当前遍历的单词是否在map里
                if (map.get(wordList[i]) == null) {
                    // 没有就放入,并设置出现一次
                    map.put(wordList[i], 1);
                } else {
                    // 如果已存在则获取当前map中这个单词的出现次数
                    int num = map.get(wordList[i]);
                    // 加一放回
                    map.put(wordList[i], ++num);
                }
            }
    
            // 存放map的key
            String[] mapKey = new String[wordList.length];
            // 存放map的value
            int[] mapValue = new int[wordList.length];
            int count = 0;
            Iterator it = map.entrySet().iterator();
            // 迭代循环储存到定义的容器中
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                mapKey[count] = (String) entry.getKey();
                mapValue[count] = (int) entry.getValue();
                count++;
            }
    
            Sort(mapKey, mapValue);
        }
    
        //创建排序方法
        public static void Sort(String[] mapKey, int[] mapValue) {
            String[] str = new String[3];
            for (int j = 0; j < mapValue.length - 1; j++)
                for (int k = 0; k < mapValue.length - j - 1; k++) {
                    if (mapValue[k] < mapValue[k + 1]) {
                        int temp1 = mapValue[k];
                        mapValue[k] = mapValue[k + 1];
                        mapValue[k + 1] = temp1;
    
                        String temp2 = mapKey[k];
                        mapKey[k] = mapKey[k + 1];
                        mapKey[k + 1] = temp2;
                    }
                }
            for (int j = 0; j < 3; j++){
                str[j] = mapKey[j] + "=" + mapValue[j];
            }
            System.out.println(str[0] + "\n" + str[1] + "\n" + str[2]);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
  • 相关阅读:
    Linux :realpath 命令
    DevOps持续集成与交付
    PHP 循环控制 学习资料
    离线安装python第三方库的实用方法:解决公司内网,服务器/电脑不能上网却需要安装python三方库问题(下:在公司是内网,有私有Pypi镜像的情况下)
    进程调度算法-时间片轮转、最高优先级和多级反馈队列调度算法
    学习记忆——宫殿篇——记忆宫殿——地点桩——演讲稿定位记忆
    英文伪原创工具-免费批量英文伪原创软件
    C. Diverse Matrix
    最小生成树模板prim和kruskal
    @EventPublisher + @Async 异步事件流详解
  • 原文地址:https://blog.csdn.net/remsqks/article/details/127822484