Java之Map循环方式

		Map<String, String> map = new HashMap<>(4);
        map.put("hello", "world");

        // 1.通过keySet
        for (String key : map.keySet()) {
            String value = map.get(key);
            System.out.println("key:" + key + ",value:" + value);
        }

        // 2.通过迭代器
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
        }

        // 3.通过Map.entrySet
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
        }

        // 4.通过lambda
        map.forEach((k, v) -> System.out.println("key:" + k + ",value:" + v));

你可能感兴趣的:(java)