---
title: "检索出文本中出现次数最多的单词"
author: "fairy_xzc"
date: "2017-09-06T21:29:27Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2017-09-06T21:35:45Z"
id: 2392423
url: "https://agilestyle.iteye.com/blog/2392423"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2392423](/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

![](http://dl2.iteye.com/upload/attachment/0126/8502/1aba8481-de03-3b17-80a9-65c0de4f2337.png)