62,628
社区成员
发帖
与我相关
我的任务
分享
String str="we come on,in the end;this is csdn";//
String[] strList=str.split(" |,|;");
Map<String,int> strMap=new Map<String,int>();
for(String s:strList){
int i=strMap.get(s);
if(i==null){
strMap.put(s,1);
} else {
strMap.put(s,i+1);
}
}
for(String key:strMap.keySet()){
System.out.println(key+"出现了 " +strMap.get(key) +"次");
}
顺便问下,新面试职位的薪水多少啊?[/quote]
试用期5000,转正6000.
//计算字符串中每一个单词出现的个数
public static Map countWords(String str){
if(str==null || str.isEmpty() ) {
System.out.println("字符串为空,无法分隔出单词");
return null;
}
String[] splitResult=str.split("[^A-Za-z]");
Map<String,Integer> map=new ConcurrentHashMap<String,Integer>();
map.put(splitResult[0].toLowerCase(),0);
for(String word:splitResult) {
Iterator< Map.Entry<String,Integer> > iterator=map.entrySet().iterator();
String wordLowerCase=word.toLowerCase();
int n=0;
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
if( wordLowerCase.equals(entry.getKey() ) ){
map.put(wordLowerCase,entry.getValue()+1);
break;
}
n++;
}
if(n==map.size() && !wordLowerCase.isEmpty()) { //如果单词和map里所有的key都不同,就把单词作为新的key加入到map里
map.put(wordLowerCase,1);
}
}
return map;
}

/**
* 统计文本每个单词的个数
* @param text 文本
* @param ignoreCase 是否忽略大小写
* @return
*/
public static Map<String ,Integer> countEachWorld(String text,boolean ignoreCase){
Matcher m=Pattern.compile("\\w+").matcher(text);
String matcheStr=null;
Map<String ,Integer> map=new LinkedHashMap<>();
Integer count=0;
while(m.find()){
matcheStr=m.group();
matcheStr=ignoreCase?matcheStr.toLowerCase():matcheStr;
count=map.get(matcheStr);
map.put(matcheStr, count!=null?count+1:1);
}
return map;
}
String str="we come on,in the end;this is csdn";//
String[] strList=str.split(" |,|;");
Map<String,int> strMap=new Map<String,int>();
for(String s:strList){
int i=strMap.get(s);
if(i==null){
strMap.put(s,1);
} else {
strMap.put(s,i+1);
}
}
for(String key:strMap.keySet()){
System.out.println(key+"出现了 " +strMap.get(key) +"次");
}
顺便问下,新面试职位的薪水多少啊?