java map遍历的5种方法

JDK8以下

foreach遍历方式【推荐写法】

public class MapDemo {
    
    
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "heyi");
        map.put(2, "heyi2");
        map.put(3, "heyi2");

        forMap(map);
    }
    //使用增强for + entrySet 进行循环
    private static void forMap(Map<Integer, String> map) {
    
    
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
    
    
            System.out.println("key  >>>>" + entry.getKey());
            System.out.println("Value >>>>>" + entry.getValue());
        }
    }
}

Iterator+entrySet写法

Map.Entry是Map接口的内部接口,获取迭代器后使用while依次取出每个迭代器里面的Map.Entry


public class MapDemo {
    
    
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "heyi");
        map.put(2, "heyi2");
        map.put(3, "heyi2");

        whileIterator(map);
    }

    //while+迭代器
    private static void whileIterator(Map<Integer, String> map) {
    
    
        //获取迭代器
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        //是否存在下一个
        while (iterator.hasNext()) {
    
    
            //获取到下一个
            Map.Entry<Integer, String> next = iterator.next();
            //输出key
            System.out.println("Key >>> " + next.getKey());
            //输出value
            System.out.println("Value >>>>>" + next.getValue());
        }
    }
}

Iterator+keyset写法

不推荐,只能获取key,然后通过key获取对应的value,重复计算


public class MapDemo {
    
    
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "heyi");
        map.put(2, "heyi2");
        map.put(3, "heyi2");
        whileKey(map);
    }

    //while+迭代器获取key
    private static void whileKey(Map<Integer, String> map) {
    
    
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
    
    
            Integer key = iterator.next();
            String value = map.get(key);
            System.out.println("Key >>>> " + key);
            System.out.println("Value >>>>>" + value);
        }
    }
}

JDK8

foreach lambda遍历方式【推荐写法 简捷】


public class MapDemo {
    
    
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "heyi");
        map.put(2, "heyi2");
        map.put(3, "heyi2");
        foreachMap(map);

    }

    private static void foreachMap(Map<Integer, String> map) {
    
    
        map.forEach((key, value) -> {
    
    
            System.out.println("key >>> " + key);
            System.out.println("value  >>>>>  " + value);
        });
    }
}

stream流遍历Map【不推荐写法,重复计算】

如果Map集合存在一些中间处理,可以过滤操作,使用流式遍历也很方便。


public class MapDemo {
    
    
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "heyi");
        map.put(2, "heyi2");
        map.put(3, "heyi2");

        streamForeach(map);
    }

    private static void streamForeach(Map<Integer, String> map) {
    
    
        map.entrySet().stream().forEach((Map.Entry<Integer, String> enTry) -> {
    
    
            System.out.println("key >>> " + enTry.getKey());
            System.out.println("value  >>>>>  " + enTry.getValue());
        });
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45178729/article/details/129057938