CopyCode The Code is as follows: Package com. gpdi. Action;
Import java. util. arraylist;
Import java. util. collections;
Import java. util. hashmap;
Import java. util. List;
Import java. util. Map;
Public class wordsstatistics {
Class 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 ){
Return NULL;
}
WORD = 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 (string simpleword: wordarray ){
Simpleword = simpleword. Trim ();
If (simpleword! = NULL &&! Simpleword. inclusignorecase ("")){
Obj cnt = map. Get (simpleword );
If (CNT! = NULL ){
CNT. Count ++;
} Else {
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 compare (wordcount O1, wordcount O2 ){
Int result = 0;
If (o1.getcount ()> o2.getcount ()){
Result =-1;
} Else if (o1.getcount () <o2.getcount ()){
Result = 1;
} Else {
Int strrs = o1.getword (). comparetoignorecase (o2.getword ());
If (strrs> 0 ){
Result = 1;
} Else {
Result =-1;
}
}
Return result;
}
});
Return Rs;
}
Public static void main (string ARGs []) {
String word = "Pinterest is might be aa AB marketer's dream-THS site is largely used to curate products ";
Wordsstatistics S = new wordsstatistics ();
List <wordcount> rs = S. Statistics (Word );
For (wordcount word1: RS ){
System. Out. println (word1.getword () + "*" + word1.getcount ());
}
}
}