The highest occurrence frequency of Java string computing characters.
Public class HighFrequencyWord {
Public static void findFrequencyWord (String str ){
Collection <Integer> al = new ArrayList <Integer> ();
Map <String, Integer> map = new HashMap <String, Integer> ();
String tempStr = str;
String [] stringArray = str. split ("");
System. out. println ("stringArray. length =" + stringArray. length );
// It is used to store each character that appears in the string without repeating
Set <String> set = new HashSet <String> ();
Int stringLength = stringArray. length;
For (int I = 0; I <stringLength; I ++ ){
Set. add (stringArray [I]);
}
// Remove an empty character from the set
Set. remove ("");
System. out. println (set );
Int count = 0;
Boolean flag = true;
For (String s: set ){
While (flag ){
// If the index exists
If (tempStr. indexOf (s )! =-1 ){
// Record the current position of the character
Int index = tempStr. indexOf (s );
// Start the string from the next Index
TempStr = tempStr. substring (index + 1 );
// If the index exists, add 1 to the record value
Count ++;
} Else {
// If the index does not exist, assign a value of false to exit the loop.
Flag = false;
}
}
// To execute the next loop
Flag = true;
// Store the value in map to match the Character Frequency
Map. put (s, count );
// As it is the previous result, the value is set to zero and the string is changed back to the original string.
Count = 0;
TempStr = str;
}
// Change the map value to a list.
Al = map. values ();
// Convert it to an array
Integer [] stringCount = al. toArray (new Integer [] {});
Int countLength = stringCount. length;
// Sort in ascending order
Arrays. sort (stringCount );
// Obtain the maximum value of the array.
Int max = stringCount [countLength-1];
For (String s: set ){
For (int I = 0; I <countLength; I ++ ){
// If the map value is the same as the maximum value, the output
If (map. get (s) = max ){
System. out. println (s + ":" + max );
}
}
}
}
Public static void main (String [] args ){
FindFrequencyWord ("ababbsssss ");
}