import java.util.*;
class Test
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("输入你的字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] arr = str.toCharArray();
getMap(arr);
}
public static void getMap(char[] arr)
{
Map<Character, Integer> map = new TreeMap<Character,Integer>();
for(char ch:arr)
{
Integer i = map.get(ch);
if(i==null)
{
map.put(ch,1);
}
else
{
i++;
map.put(ch,i);
}
}
StringBuilder sb = new StringBuilder();
Set<Character> set = map.keySet();
for(char c:set)
{
sb.append(c).append(map.get(c)).append("\t");
System.out.println(sb);
}
}
}
|
|