统计英文字符串里重复次数最多的单词JAVA
程序员文章站
2022-07-05 21:05:12
用 map来统计各个单词的次数用list存储出现过的单词,后面出现的同样的单词就不用放进去了求出现最多次的单词从list里面找字符串对应map里的s public static void main(String[] args) { int max = 0; Map map = new HashMap(); List strings...
用 map来统计各个单词的次数
用list存储出现过的单词,后面出现的同样的单词就不用放进去了
求出现最多次的单词从list里面找字符串对应map里的s
public static void main(String[] args) {
int max = 0;
Map<String,Integer> map = new HashMap<String, Integer>();
List<String> strings = new ArrayList<String>();
String[] str = "Donald Trump has carried the crucial battleground states of Florida, Texas Ohio and Iowa but Joe Biden has won Arizona The contests in Michigan and Wisconsin are close as postal votes are counted Because of the many postal ballots cast in this election some states’ results may not be known for days or even weeks More than 100 million Americans voted early or by post suggesting a record turnout Democrats’ hopes gaining control of the Senate are fading after Republicans held on to closely fought seats in South Carolina and Iowa Control of the Senate may come down to a Georgia special election that will be decided in a run-off in January".toLowerCase().split(" |, |\\.");
for(String s : str){
if(map.containsKey(s)){
map.put(s,map.get(s)+1);
}else{
map.put(s,1);
strings.add(s);
}
}
for(String s :str){
if(max < map.get(s))
max = map.get((s));
}
for(String s : strings){
if(max == map.get(s)){
System.out.println(s+"出现了"+max+"次");
}
}
}
本文地址:https://blog.csdn.net/wgajc4840/article/details/109612292
下一篇: POI导出之我的实践篇