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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© LARK 中级黑马   /  2016-10-12 16:30  /  2378 人查看  /  22 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Map集合的四种遍历方式,分享:   
        Map<Student, String> hm = new HashMap<Student, String>();
                hm.put(new Student("张三",23), "北京");
                hm.put(new Student("李四",23), "上海");
                hm.put(new Student("王五",24), "广州");
                hm.put(new Student("赵六",25), "深圳");
        //第一种方式:获取所有键的集合,再使用迭代器
                Set<Student> set = hm.keySet();
                Iterator<Student> it1 = set.iterator();
                while(it1.hasNext()){
                        Student key = it1.next();
                        String value = hm.get(key);
                        System.out.println(key + "::" + value);
                }
                System.out.println("-----------------------------------");
        //第二种方式:用keySet()方法获取所有键的集合,再利用增强for
                for(Student key : hm.keySet()){
                        System.out.println(key + "::" + hm.get(key));
                }
                System.out.println("-----------------------------------");
        //第三种方式:获取键值对对象集合,再获取迭代器遍历,再getKey();getValue();
                Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
                Iterator<Map.Entry<Student, String>> it2 = entrySet.iterator();
                while(it2.hasNext()){
                        Map.Entry<Student, String> en = it2.next();
                        Student key = en.getKey();
                        String value = en.getValue();
                        System.out.println(key + "::" + value);
                }
                System.out.println("-----------------------------------");
        //第四种方式:获取键值对对象集合,再用增强for遍历
           即:把原Map双列集合的每一对键值对包装成一个类似Person的对象):Map.Entry(Entry是Map的静态内部类)
                Set<Map.Entry<Student, String>> entrySet2 = hm.entrySet();
                for (Map.Entry<Student, String> entry : entrySet2) {
                        System.out.println(entry.getKey() + "::" + entry.getValue());
                }

22 个回复

倒序浏览
map的键只能是string类型吗不是
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
黑马是马 发表于 2016-10-12 17:05
map的键只能是string类型吗不是

谁告诉你的?正常是自己定义的泛型,一般Properties的键值的泛型默认为String类型。
回复 使用道具 举报
黑马是马 发表于 2016-10-12 17:05
map的键只能是string类型吗不是

不是,可以是任意类型的,包括null
回复 使用道具 举报
长见识啊
回复 使用道具 举报
黑马是马 发表于 2016-10-12 17:05
map的键只能是string类型吗不是

,你引起公愤了。
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
回复 使用道具 举报
其实方式都差不多
回复 使用道具 举报
chensc 金牌黑马 2016-10-13 20:54:16
9#
学习学习!
回复 使用道具 举报
黑马是马 发表于 2016-10-12 17:05
map的键只能是string类型吗不是

你是猴子派来的逗B么
回复 使用道具 举报
学习学习!学习学习!学习学习!学习学习!学习学习!学习学习!学习学习!
回复 使用道具 举报
我必须来顶顶贴
回复 使用道具 举报
顶一个,前几天刚学完
回复 使用道具 举报
我可以告诉你增强for的底层就是迭代嘛。
回复 使用道具 举报
学习了{:2_31:}
回复 使用道具 举报
来看看不错噢
回复 使用道具 举报
LARK 中级黑马 2016-10-14 00:36:25
17#
IceLoveInFire丶 发表于 2016-10-13 23:07
我可以告诉你增强for的底层就是迭代嘛。

我知道啊
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
LShu 中级黑马 2016-10-14 00:45:34
18#
路过学习...
回复 使用道具 举报
jinjinjin 发表于 2016-10-13 21:08
你是猴子派来的逗B么

不知道所以才问啊,大哥
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马