- import java.util.Comparator;
- import java.util.HashMap;
- public class Demo3_TreeMap {
- public static void main(String[] args) {
- String s = "aaaaabbbbbbbcccc"; //定义一个字符串
- char [] arr = s.toCharArray(); //将字符串转换为字符数组
- HashMap<Character, Integer> hm = new HashMap<>(); //定义hashmap集合
- for(char c : arr) { //遍历字符串
- if (!hm.containsKey(c)) { //判断出现的次数
- hm.put(c, 1);
- }else {
- hm.put(c, hm.get(c)+1);
- }
- for (Character key : hm.keySet()) {
- System.out.println(key + "=" + hm.get(key));
- }
- }
- }
- }
复制代码
|
|