• java LinkedHashMap,TreeMap,HashMap


     1     /**
     2  3      * 需求:统计字符串中每个字符出现的次数
     4      * 
     5      * 分析:
     6      * 1,定义一个需要被统计字符的字符串
     7      * 2,将字符串转换为字符数组
     8      * 3,定义双列集合,存储字符串中字符以及字符出现的次数
     9      * 4,遍历字符数组获取每一个字符,并将字符存储在双列集合中
    10      * 5,存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
    11      * 6,打印双列集合获取字符出现的次数
    12      */
    13     public static void main(String[] args) {
    14         //1,定义一个需要被统计字符的字符串
    15         String s = "aaaabbbbbccccccccccccc";
    16         //2,将字符串转换为字符数组
    17         char[] arr = s.toCharArray();
    18         //3,定义双列集合,存储字符串中字符以及字符出现的次数
    19         HashMap<Character, Integer> hm = new HashMap<>();
    20         //4,遍历字符数组获取每一个字符,并将字符存储在双列集合中
    21         for(char c: arr) {
    22             //5,存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
    23             /*if(!hm.containsKey(c)) {            //如果不包含这个键
    24                 hm.put(c, 1);
    25             }else {
    26                 hm.put(c, hm.get(c) + 1);
    27             }*/
    28             hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);
    29         }
    30         //6,打印双列集合获取字符出现的次数
    31         
    32         for (Character key : hm.keySet()) {                //hm.keySet()代表所有键的集合
    33             System.out.println(key + "=" + hm.get(key));//hm.get(key)根据键获取值
    34         }
    35     }
     
    
    

      

    hashmap  嵌套  hashmap 
    HashMap<Student, String> hm88 = new HashMap<>();
    hm88.put(new Student("张三", 23), "北京");
    hm88.put(new Student("李四", 24), "北京");
    hm88.put(new Student("王五", 25), "上海");
    hm88.put(new Student("赵六", 26), "广州");


    HashMap<Student, String> hm99 = new HashMap<>();
    hm99.put(new Student("唐僧", 1023), "北京");
    hm99.put(new Student("孙悟空",1024), "北京");
    hm99.put(new Student("猪八戒",1025), "上海");
    hm99.put(new Student("沙和尚",1026), "广州");

    //定义双元课堂
    HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
    hm.put(hm88, "第88班");
    hm.put(hm99, "第99班");

    //遍历双列集合
    for(HashMap<Student, String> h : hm.keySet()) { //hm.keySet()代表的是双列集合中键的集合
    String value = hm.get(h); //get(h)根据键对象获取值对象
    //遍历键的双列集合对象
    for(Student key : h.keySet()) { //h.keySet()获取集合总所有的学生键对象
    String value2 = h.get(key);

    System.out.println(key + "=" + value2 + "=" + value);
    }
    }

  • 相关阅读:
    使用SpringMVC的crud操作时,进行数据修改,但是修改成功后,页面无法显示lastName属性值(被修改的那条记录)
    使用SpringMVC时报错HTTP Status 405
    使用springmvc时报错org.springframework.beans.NullValueInNestedPathException: Invalid property 'department' of bean class [com.atguigu.springmvc.crud.entities.Employee]:
    快速傅里叶变换应用之二 hdu 4609 3-idiots
    离散傅立叶变换与快速傅立叶变换(DFT与FFT)
    深度优先搜索(DFS)递归形式改为非递归形式
    模拟题组
    STL--自定义类型的排序
    多关键字的快速排序
    二叉树的前序和中序得到后序 hdu1710
  • 原文地址:https://www.cnblogs.com/yimian/p/6526797.html
Copyright © 2020-2023  润新知