• 统计英文字符串中字符出现的次数(更新中)


    package 小工具;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * @author 无法手执玫瑰
     * 2020/11/0030 17:16
     */
    public class 统计各字符出现的次数 {
        static void fun1(String s){
            char[] chars = s.toCharArray();
            //26个字母
            int[] alphabetCount = new int[26];
            for (char aChar : chars) {
                alphabetCount[aChar - 'a']++;
            }
            String s1 = Arrays.toString(alphabetCount);
            System.out.println(s1);
        }
    
        static void  fun2(String s){
            char[] chars = s.toCharArray();
            Map<Character, Integer> map = new HashMap<>();
            for (char aChar : chars) {
                map.put(aChar,map.getOrDefault(aChar,0) + 1);
            }
            /*Set<Map.Entry<Character, Integer>> entries = map.entrySet();
            entries.forEach(System.out::println);*/
            for (Map.Entry<Character, Integer> entry: map.entrySet()){
                System.out.println(entry.getKey() + "   " + entry.getValue());
            }
        }
        public static void main(String[] args) {
            String s = "aabbdcgs";
            fun2(s);
        }
    }
    
    
  • 相关阅读:
    git 常用命令速查表
    Git常用命令备忘
    display:inline-block
    JS 封装的结构关系
    IE6,7,8 CSS HACK
    JS 观察者模式
    JS 数组迭代方法
    JS 单例模式
    JS Closure 闭包
    Currying and Uncurrying Js
  • 原文地址:https://www.cnblogs.com/wfszmg/p/14062598.html
Copyright © 2020-2023  润新知