Map遍历的四种方式

java遍历map

四种方式如下,entryset,keyset,iterator,所有values。第一种方式效率高推荐使用。

        for (Map<String, Object> map : reslist) {
            log.info(map.toString());
            //one entryset
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                log.info("key :" + entry.getKey() + " " + "value " + entry.getValue());
            }
            //two keyset
            for (String key : map.keySet()) {
                log.info("key :" + key + " " + "value " + map.get(key));
            }
            //three iterator
            Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                log.info("key " + entry.getKey() + " " + "value " + entry.getValue());
            }
            //four only values
            for (Object value : map.values()) {
                log.info("value: " + value);
            }
        }
posted @ 2019-07-08 19:47  碧海潮心  阅读(194)  评论(0编辑  收藏  举报