A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© li_zis 中级黑马   /  2015-10-12 20:46  /  173 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

HashMap<String, Integer> hm = new HashMap<>();
                        hm.put("张三", 23);
                        hm.put("李四", 24);
                        hm.put("王五", 25);
                        hm.put("赵六", 26);
                       
                        /*Set<String> keySet = hm.keySet();                        //获取集合中所有的键
                        Iterator<String> it = keySet.iterator();        //获取迭代器
                        while(it.hasNext()) {                                                //判断单列集合中是否有元素
                                String key = it.next();                                        //获取集合中的每一个元素,其实就是双列集合中的键
                                Integer value = hm.get(key);                        //根据键获取值
                                System.out.println(key + "=" + value);        //打印键值对
                        }*/
                       
                        for(String key : hm.keySet()) {                                //增强for循环迭代双列集合第一种方式
                                System.out.println(key + "=" + hm.get(key));
                        }
       
###18.04_集合框架(Map集合的遍历之键值对对象找键和值)
* A:键值对对象找键和值思路:
        * 获取所有键值对对象的集合
        * 遍历键值对对象的集合,获取到每一个键值对对象
        * 根据键值对对象找键和值
* B:案例演示
        * Map集合的遍历之键值对对象找键和值
       
                        HashMap<String, Integer> hm = new HashMap<>();
                        hm.put("张三", 23);
                        hm.put("李四", 24);
                        hm.put("王五", 25);
                        hm.put("赵六", 26);
                        /*Set<Map.Entry<String, Integer>> entrySet = hm.entrySet();        //获取所有的键值对象的集合
                        Iterator<Entry<String, Integer>> it = entrySet.iterator();//获取迭代器
                        while(it.hasNext()) {
                                Entry<String, Integer> en = it.next();                                //获取键值对对象
                                String key = en.getKey();                                                                //根据键值对对象获取键
                                Integer value = en.getValue();                                                        //根据键值对对象获取值
                                System.out.println(key + "=" + value);
                        }*/
                       
                        for(Entry<String,Integer> en : hm.entrySet()) {
                                System.out.println(en.getKey() + "=" + en.getValue());
                        }
               
C:源码分析

###18.05_集合框架(HashMap集合键是Student值是String的案例)
* A:案例演示
        * HashMap集合键是Student值是String的案例

###18.06_集合框架(LinkedHashMap的概述和使用)
* A:案例演示
        * LinkedHashMap的特点
                * 底层是链表实现的可以保证怎么存就怎么取

###18.07_集合框架(TreeMap集合键是Student值是String的案例)
* A:案例演示
        * TreeMap集合键是Student值是String的案例

###18.08_集合框架(统计字符串中每个字符出现的次数)
* A:案例演示
        * 需求:统计字符串中每个字符出现的次数
                        String str = "aaaabbbcccccccccc";
                        char[] arr = str.toCharArray();                                                //将字符串转换成字符数组
                        HashMap<Character, Integer> hm = new HashMap<>();        //创建双列集合存储键和值
                       
                        for(char c : arr) {                                                                        //遍历字符数组
                                /*if(!hm.containsKey(c)) {                                                //如果不包含这个键
                                        hm.put(c, 1);                                                                //就将键和值为1添加
                                }else {                                                                                        //如果包含这个键
                                        hm.put(c, hm.get(c) + 1);                                        //就将键和值再加1添加进来
                                }
                               
                                //hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);
                                Integer i = !hm.containsKey(c) ? hm.put(c, 1) : hm.put(c, hm.get(c) + 1);
                                                }
                       
                        for (Character key : hm.keySet()) {                                        //遍历双列集合
                                System.out.println(key + "=" + hm.get(key));
                        }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马