Map 遍历

使用EntrySet和Entry

Map<String,String> map=new HashMap<String,String>();
        map.put("A", "a");
        map.put("B", "b");
        map.put("C", "c");
        Set set=map.entrySet();
        for (Object object : set) {
            Entry entry=(Entry)object;
            System.out.println(entry.getKey()+":"+entry.getValue());
        }

 

使用迭代器Iterator

Map map=new HashMap();
        map.put("A", "a");
        map.put("B", "b");
        map.put("C", "c");
        Set set=map.keySet();
        Iterator it=set.iterator();
        Object key;
        Object value;
        while(it.hasNext()){
            key=it.next();
            value=map.get(key);
            System.out.println(key+":"+value);
        }

posted on 2016-10-28 11:28  flovato  阅读(101)  评论(0编辑  收藏  举报

导航