我是靠谱客的博主 花痴砖头,这篇文章主要介绍hashMap的三种遍历方式,现在分享给大家,希望可以做个参考。


    public void testClass(){
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("1", "1");
        hashMap.put("2", "2");
        Set<Entry<String, String>> entrySet = hashMap.entrySet();
        Iterator<Entry<String, String>> iterator = entrySet.iterator();
        //1.迭代器实现 效率高
        while(iterator.hasNext()){
            Entry<String, String> entry = iterator.next();
            String key = entry.getKey();
            System.out.println(key);
            String value = entry.getValue();
            System.out.println(value);
        }
        //2.entrySet
        for (Entry<String, String> entry : entrySet) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        //3.keySet
        Set<String> keySet = hashMap.keySet();
        for (String entry : keySet) {
            System.out.println(hashMap.get(entry));
        }
    }

最后

以上就是花痴砖头最近收集整理的关于hashMap的三种遍历方式的全部内容,更多相关hashMap内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(126)

评论列表共有 0 条评论

立即
投稿
返回
顶部