导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共类WordsStatistics {
类 Obj {
整数计数;
对象(整数计数){
this.count = 计数;
}
}
公共列表 <WordCount> 统计信息(字符串单词){
List<WordCount> rs = new ArrayList<WordCount>();
Map <String,Obj> map = new HashMap<String,Obj>();
如果(字==空){
返回空值;
}
字 = word.toLowerCase();
word = word.replaceAll("'s", "");
word = word.replaceAll(",", "");
word = word.replaceAll("-", "");
word = word.replaceAll("//.", "");
word = word.replaceAll("'", "");
word = word.replaceAll(":", "");
word = word.replaceAll("!", "");
word = word.replaceAll("/n", "");
String[] wordArray = word.split(" ");
for(字符串 simpleWord : wordArray) {
simpleWord = simpleWord.trim();
if (simpleWord != null && !simpleWord.equalsIgnoreCase("")) {
Obj cnt = map.get(simpleWord);
如果(cnt!= null){
cnt.count++;
}别的 {
map.put(simpleWord, new Obj(1));
}
}
}
for(字符串键:map.keySet()) {
WordCount wd = new WordCount(key,map.get(key).count);
rs.add(wd);
}
Collections.sort(rs, new java.util.Comparator<WordCount>(){
@覆盖
公共 int 比较(字数 o1,字数 o2){
整数结果 = 0 ;
if (o1.getCount() > o2.getCount() ) {
结果=-1;
}否则 if (o1.getCount() < o2.getCount()) {
结果=1;
}别的 {
int strRs = o1.getWord().compareToIgnoreCase(o2.getWord());
如果(strRs> 0){
结果=1;
}别的 {
结果=-1;
}
}
返回结果;
}
});
返回RS;
}
公共静态无效主(字符串参数[]){
String word =“Pinterest 可能是 aa ab aa ab 营销人员的梦想 - 该网站主要用于策划产品”;
WordsStatistics s = new WordsStatistics();
List<WordCount> rs = s.statistics(word);
for(WordCount word1 : rs) {
System.out.println(word1.getWord()+"*"+word1.getCount());
}
}
}