1、直接上代码
public class CheckWord {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一串字符:");
String word = input.next();
showWord(word);
}
private static void showWord(String word) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < word.length(); i++) {
char atr = word.charAt(i);
if (!map.containsKey(atr)) {
map.put(atr, 1);
} else {
int count = map.get(atr);
map.put(atr, count + 1);
}
}
Set<Map.Entry<Character, Integer>> entrySets = map.entrySet();
for (Map.Entry<Character, Integer> entry : entrySets) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
2、输出打印结
请输入一串字符:
alibababilibili
a : 3
b : 4
i : 5
l : 3