刷题模板 | map

遍历:

1)遍历HashMap的entrySet键值对集合

Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            key = (String) entry.getKey();
            value = (String) entry.getValue();
            System.out.println("key:" + key + "---" + "value:" + value);
        }

2)遍历HashMap键的Set集合获取值

Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            key = (String) it.next();
            value = (String) map.get(key);
            System.out.println("key:" + key + "---" + "value:" + value);
        }

3)

// 获取值集合的迭代器
        Iterator it = map.values().iterator();
        while (it.hasNext()) {
            value = (String) it.next();
            System.out.println("value:" + value);
        }
发布了53 篇原创文章 · 获赞 5 · 访问量 1514

猜你喜欢

转载自blog.csdn.net/zhicheshu4749/article/details/104091213
Map