【Java】遍历Map<String,String>

        Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");

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

        //通过Map.keySet遍历key和value
        for (String key : map.keySet()) {
            System.out.println("key= "+ key + " and value= " + map.get(key));
        }
        
        //通过Map.values()遍历所有的value,但不能遍历key
        for (String v : map.values()) {
            System.out.println("value= " + v);
        }

 

posted @ 2019-10-22 15:58    阅读(2061)  评论(0编辑  收藏  举报