检索出文本中出现次数最多的单词

原创转载请注明出处:http://agilestyle.iteye.com/blog/2392423

 

具体思路,将文本中的段落进行split拆分成一个个单词,然后遍历,放入map,统计每个单词出现的次数,最后放入list,按出现次数进行降序排列

Note:此方法不是最优解,还有更高效的算法,欢迎拍砖

 

package org.fool.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        String str = "Ah, Ah, Ah, What the hell are you talking about, I have already told you what you should do, I really have no idea what you think about";
        String[] strings = str.replace('\'', ' ').replace(',', ' ').replace('.', ' ').split("\\s+");

        Map<String, Integer> map = new HashMap<>();
        for (String s : strings) {
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }

        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());

        list.sort((o1, o2) -> o2.getValue() - o1.getValue());

        System.out.println(list.get(0).getKey() + ":" + list.get(0).getValue());
        System.out.println(list.get(1).getKey() + ":" + list.get(1).getValue());
    }
}

Console Output


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值