[Java] traverse Map <String, String>

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

        // by Map.entrySet traversal key and a value 
        for (of Map.Entry <String, String> entry: EnumMap.entrySet ()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }

        // by Map.keySet traversal key and a value 
        for (String key: map.keySet ()) {
            System.out.println("key= "+ key + " and value= " + map.get(key));
        }
        
        // loop through all of the value by Map.values (), but can not traverse the Key 
        for (String v: map.values ()) {
            System.out.println("value= " + v);
        }

 

Guess you like

Origin www.cnblogs.com/jxd283465/p/11720324.html