- package Pratice_day18;
- import java.util.HashMap;
- public class HashMapTest2 {
- public static void main(String[] args) {
- String s="cbxzbvavdvgd";
- //必须转化成引用类型
- //int类型会报错
- HashMap<Character,Integer> map = new HashMap<Character,Integer>();
- // 将字符串转化成字符数组
- char[] chs=s.toCharArray();
- // 遍历数组 增强for
- for (char c : chs) {
- if (map.containsKey(c)) {
- map.put(c, 1);
- }
- else if(!(map.containsKey(c))){
- map.put(c,map.get(c)+1);
- }
- }
- System.out.println(map);
- }
- }
复制代码
|
|