java字符串中重复单词_统计英文字符串里重复次数最多的单词JAVA

用 map来统计各个单词的次数

用list存储出现过的单词,后面出现的同样的单词就不用放进去了

求出现最多次的单词从list里面找字符串对应map里的s

public static void main(String[] args) {

int max = 0;

Map map = new HashMap();

List strings = new ArrayList();

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值