import java.util.ArrayList;
java.util.Collections importieren;
import java.util.HashMap;
java.util.List importieren;
java.util.Map importieren;
öffentliche Klasse WordsStatistics {
Klasse Obj {
int count ;
Obj(int count){
this.count = count;
}
}
public List<WordCount> Statistics(String Word) {
List<WordCount> rs = new ArrayList<WordCount>();
Map <String,Obj> map = new HashMap<String,Obj>();
if(word == null ) {
null zurückgeben;
}
Wort = Wort.toLowerCase();
word = word.replaceAll("'s", "");
Wort = Wort.replaceAll(",", "");
word = word.replaceAll("-", "");
word = word.replaceAll("//.", "");
word = word.replaceAll("'", "");
Wort = Wort.replaceAll(::, "");
word = word.replaceAll("!", "");
word = word.replaceAll("/n", "");
String [] wordArray = word.split(" ");
for(String simpleWord : wordArray) {
simpleWord = simpleWord.trim();
if (simpleWord != null && !simpleWord.equalsIgnoreCase("")) {
Obj cnt = map.get(simpleWord);
if ( cnt!= null ) {
cnt.count++;
}anders {
map.put(simpleWord, new Obj(1));
}
}
}
for(String key : map.keySet()) {
WordCount wd = new WordCount(key,map.get(key).count);
rs.add(wd);
}
Collections.sort(rs, new java.util.Comparator<WordCount>(){
@Override
public int vergleichen(WordCount o1, WordCount o2) {
int result = 0 ;
if (o1.getCount() > o2.getCount() ) {
Ergebnis = -1;
}else if (o1.getCount() < o2.getCount()) {
Ergebnis = 1;
}anders {
int strRs = o1.getWord().compareToIgnoreCase(o2.getWord());
if ( strRs > 0 ) {
Ergebnis = 1;
}anders {
Ergebnis = -1 ;
}
}
Ergebnis zurückgeben;
}
});
rs zurückgeben;
}
public static void main(String args[]) {
String word = „Pinterest könnte der Traum eines AA-AB-Vermarkters sein – diese Website wird hauptsächlich zum Kuratieren von Produkten genutzt“;
WordsStatistics s = new WordsStatistics();
List<WordCount> rs = s.statistics(word);
for(WordCount word1 : rs) {
System.out.println(word1.getWord()+"*"+word1.getCount());
}
}
}