Map集合的遍历

第一种  map.entrySet()

public class Main1 {

    public static void main(String[] args){

        Map<String, String> map = new HashMap();

        map.put("key", "demo");

        map.put("key1", "demo1");

        for (Map.Entry<String, String> a : map.entrySet()) {

            System.out.println(a.getKey() + "=======" + a.getValue());

        }

    }

}

第二种  Iterator迭代器

public class Main1 {

    public static void main(String[] args) {

        Map<String, String> map = new HashMap();

        map.put("key", "demo");

        map.put("key1", "demo1");

        map.put("key2", "demo2");

        Iterator iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {

            Map.Entry entry = (Map.Entry) iterator.next();

            System.out.println(entry.getKey() + "=====" + entry.getValue());

        }

    }

}

第三种  个人认为巨笨的方法 

public class Main1 {

    public static void main(String[] args) {

        Map<String, String> map = new HashMap();

        map.put("key", "demo");

        map.put("key1", "demo1");

        map.put("key2", "demo2");

        Iterator iterator = map.keySet().iterator();

        while (iterator.hasNext()) {

            //取出key

            String key = iterator.next().toString();

            System.out.println(key);

            //通过key拿到value

            String str = map.get(key);

            System.out.println(str);

        }

    }

}

————————————————

版权声明:本文为CSDN博主「奈何_smail」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_42081445/article/details/105126856

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容