CopyCode The Code is as follows :/*
String name = "adsbsadgsadgtewterfsdf ";
Eg a --> 6, B --> 1 d --> 3...
Store the string in A (letter) => 2 (number) to the map set framework.
Idea: 1. convert a string into a character array.
2. define a map set and traverse the character array. If the map set does not contain this element, the element is saved to the map set, and a counter is defined to store the number of times to 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; X ++)
{
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;
}
}