import java.util.Set;
import java.util.TreeMap;
//获取一个字符串"sdfaswefavzvezvrzzvrszzcewe"中字符的个数,并以a(1)b(2)...的形式输出。
public class TreeMapSortDemo {
public static void main(String[] args) {
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
String s ="sdfaswefavzvezvrzzvrszzcewe";
char[] ch = s.toCharArray();
for (char c : ch) {
Integer i = tm.get(c);
if(i!=null){
i++;
tm.put(c,i);
}else{
tm.put(c, 1);
}
}
StringBuilder sb = new StringBuilder();
Set<Character> keySet = tm.keySet();
for (Character key : keySet) {
Integer i =tm.get(key);
sb.append(key+"("+i+")");
}
System.out.println(sb);
}
} |
|