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]);
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值