举例说明: String content = “博博客333ccsssnn”;
统计出:
-
客:1 3:3 c:2 s:3 n:2
public static void main(String[] args) {
String content = "博博客333ccsssnn";
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
while (content.length() > 0) {
Character c = content.charAt(0);
content = content.substring(1);
Integer count = map.get(c);
if (count == null) {
map.put(c, 1);
} else {
map.put(c, count + 1);
}
}
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
System.out.println(entry);
}
}