Copy Code code as follows:
/*
String name = "ADSBSADGSADGTEWTERFSDF";
Eg a-->6,b-->1 d-->3 ...
The string is stored in the map collection frame with a (letter) =>2 (number)
Train of thought: 1. Converts a string into a character array.
2. Define a map collection, and then iterate over the character array, if the element is not in the map collection, the element is stored in the map set, and a counter is defined, and the number is stored in the map to achieve the goal
*/
Import java.util.*;
Class Charmapdemo
{
public static void Main (string[] args)
{
String str = "ABCEDSADGSADFASDFAA";
treemap<character,integer> TM = Pross (str);
SYSTEM.OUT.PRINTLN (tm);
}
public static treemap<character,integer> Pross (String str)
{
char[] Chararray = Str.tochararray ();
treemap<character,integer> TM = new treemap<character,integer> ();
for (int x = 0; x < Chararray.length + +)
{
if (!tm.containskey (Chararray[x]))
{
Tm.put (chararray[x],1);
}
Else
{
int count = Tm.get (chararray[x]) +1;
Tm.put (Chararray[x],count);
}
}
return TM;
}
}