public class 遍历字符串 {
public static void main(String[] args) {
String s = "cbxzbvavdvgd";
char[] chs = s.toCharArray();
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
for(char ch : chs){
Integer i = tm.get(ch);
if(i==null){
tm.put(ch, 1);
}
else{
i++;
tm.put(ch, i);
}
}
StringBuilder sb = new StringBuilder();
Set<Character> ss = tm.keySet();
for(Character key: ss){
Integer i = tm.get(key);
sb.append(key).append("(").append(i).append(")");
}
System.out.println(sb);
}
} |
|