Map遍历的四种方式

java遍历map

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

        for (Map map : reslist) {
            log.info(map.toString());
            //one entryset
            for (Map.Entry 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> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = iterator.next();
                log.info("key " + entry.getKey() + " " + "value " + entry.getValue());
            }
            //four only values
            for (Object value : map.values()) {
                log.info("value: " + value);
            }
        }

你可能感兴趣的:(Map遍历的四种方式)