public class work1 { public static void main(String[] args) { //创建一个数组,存入一串字符串 String a =new String("asdsfgdfgh"); HashMap<Character, Integer> map = new HashMap<>(); char c; for (int i = 0; i <a.length() ; i++) { c = a.charAt(i); if (!map.containsKey(c)){ map.put(c,1); }else { Integer b =map.get(c); b++; map.put(c,b); } } System.out.println(map); } }
运行结果如图: