Package Javastudy;import Java.util.arraylist;import Java.util.collections;import java.util.iterator;import Java.util.treeset;public class Test1 {public static void main (string [] args) {string str = "Aacdggdhrdhgsjrsovoiwimtcpiq" ; New Test1 (). dostring (str);} public void dostring (String str) {char [] chars = Str.tochararray (); arraylist<string> lists = new arraylist<string> (); treeset<string> set = new treeset<string> (); for (int i=0;i<chars.length;i++) {Lists.add (String.valueOf (Chars[i])); Set.add (string.valueof (Chars[i));} Order and no repetition, find all occurrences of System.out.println ("set:" +set); Order can have repetition, ArrayList can quickly sort collections.sort (lists); System.out.println ("lists:" +lists);//Gets the sorted string StringBuffer sb = new StringBuffer (); for (int i=0;i<lists.size (); i+ +) {sb.append (Lists.get (i));} str = sb.tostring (); System.out.println ("str after sorted:" +STR);//Statistics The first occurrence of the most occurrences of char, noting that the maximum number of occurrences is Max, and the corresponding char is maxstringint max = 0; String maxstring = ""; arraylist<string> maxlist = new ArraylIst<string> (); Iterator ITR = Set.iterator (); while (Itr.hasnext ()) {string os= (String) itr.next (); int begin = Str.i Ndexof (OS), int end = Str.lastindexof (OS), int value = End-begin+1;if (Value>max) {max= value; maxstring = OS; maxlist.a DD (OS);} else if (value = = max) {maxlist.add (OS);}} Find the largest position int index = 0;for (int i=0;i<maxlist.size (); i++) {if (Maxlist.get (i). Equals (maxstring)) {index =i;break;}} SYSTEM.OUT.PRINTLN ("Max Data"), for (int i=index;i<maxlist.size (); i++) {System.out.println (Maxlist.get (i) + "");} System.out.println (); System.out.println ("Max:" + max);}}
The output result is
Set:[a, C, D, G, H, I, J, M, O, p, Q, R, S, T, V, W]
Lists:[a, A, C, C, D, D, D, G, G, G, H, H, I, I, I, J, M, O, O, p, Q, R, R, s, S, T, V, W]
STR after SORTED:AACCDDDGGGHHIIIJMOOPQRRSSTVW
Max data
D
G
I
Count the number of letters in the string that have the most occurrences, and if there are multiple duplicates, find out