-------------------- Android training, Java training, and hope to communicate with you! ----------------------
Package CN. itcast. statstring. question7; import Java. util. *; import Java. util. map. entry; import COM. sun. XML. internal. BIND. v2.schemagen. XMLSCHEMA. list;/* 7. count the number of occurrences of each character in the "abadcdffbaeba" string, sort by number of times, and output. Example: C: 1, E: 1, D: 2, F: 2, B: 3,: 4 */public class statstring {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubstring STR = "abadcdffbaeba"; system. out. println (charcount (STR);} public static string charcount (string Str) {char [] CH = Str. tochararray (); treemap <character, integer> map = new treemap <character, integer> (); For (INT I = 0; I <ch. length; I ++) {integer value = map. get (CH [I]); If (value = NULL) {map. put (CH [I], 1) ;}else {value = value + 1; map. put (CH [I], value) ;}} arraylist <map. entry <character, integer> infoidslist = new arraylist <map. entry <character, integer> (map. entryset (); collections. sort (infoidslist, new mycomparator (); stringbuilder sbuilder = new stringbuilder (); For (INT I = 0; I <infoidslist. size (); I ++) {// system. out. println (infoidslist. get (I ). tostring (); sbuilder. append (infoidslist. get (I ). getkey () + ":" + infoidslist. get (I ). getvalue (). tostring () + ",");} string result = sbuilder. deletecharat (sbuilder. length ()-1 ). tostring (); return result ;}}
---------------------- Android training, Java training, and hope to communicate with you! ----------------------