/*
* 练习:
* "fdgavcbsacdfs" 获取该字符串中,每一个字母出现的次数。
* 要求打印结果是:a(2)b(1)...;
*
*
*/
public class MapTest {
/**
* @param args
*/
public static void main(String[] args) {
String str = "fdg+avAdc bs5dDa9c-dfs";
String s = getCharCount(str);
System.out.println(s);
}
public static String getCharCount(String str) {
// 将字符串变成字符数组
char[] chs = str.toCharArray();
// 定义map集合表。
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for (int i = 0; i < chs.length; i++) {
if (!(chs[i] >= 'a' && chs[i] <= 'z' || chs[i] >= 'A'
&& chs[i] <= 'Z'))
// if(!(Character.toLowerCase(chs[i])>='a' &&
// Character.toLowerCase(chs[i])<='z'))
continue;
// 将数组中的字母作为键去查map表。
Integer value = map.get(chs[i]);
int count = 1;
// 判断值是否为null.
if (value != null) {
count = value + 1;
}
// count++;
map.put(chs[i], count);
}
return mapToString(map);
}
private static String mapToString(Map<Character, Integer> map) {
StringBuilder sb = new StringBuilder();
Iterator<Character> it = map.keySet().iterator();
while (it.hasNext()) {
Character key = it.next();
Integer value = map.get(key);
sb.append(key + "(" + value + ")");
}
return sb.toString();
}
}