原创转载请注明出处: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