一大段字符串,各个单词之间用空格间隔,求各个单词出现的次数
具体代码:
public static void main(String[] args){
String s="this is a book that is a desk";
String[] ss=s.split(" ");
Map<String,Integer> map=new HashMap<String,Integer>();
for(String sss:ss){
if(!map.containsKey(sss)){
map.put(sss,1);
}else{
Integer in=map.get(sss);
map.replace(sss,in,in+1);
}
}
for(Map.Entry<String,Integer> entry:map.entrySet()){
System.out.println(entry.getKey()+"出现了"+entry.getValue()+"次");
}
}