/**
* 给定一个字符串 统计这个字符串中每个字母出现了多少次
* ishjdopigfuwoehfouwehifoliwdensolfuiyedwol
*
* map<Char,Integer>
*/
/
*需求分析: 给定一个字符串 统计这个字符串中每个字母出现了多少次
package com.baidu.demo2;
import java.util.HashMap;
public class CharCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
//获取键盘输入
HashMap<Character, Integer>hashmap = new HashMap<>();
//其实hashset和hashmap本质上一个东西 都可以去重
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(hashmap.containsKey(c)){
int count = hashmap.get(c).intValue();
count++;
hashmap.put(c,count);
}else{
hashmap.put(c,1);
}
}
System.out.println(hashmap);
}
}
运行结果