전공수업/JAVA

[JAVA] 자바 기초 - MAP key-value 출력 방법

Alkaloid 2022. 12. 2. 16:54
반응형
        Map<String, String> map = new HashMap<String, String>();
        map.put("map", "kim");
        map.put("name", "kim");
        map.put("age", "18");
        map.put("address", "seoul");
 
        // 첫번째 : entrySet()
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key : " + entry.getKey() + ", value :" + entry.getValue());
        }
        
        // 두번째 : keySet()
        for (String key : map.keySet()) {
            String value = map.get(key);
            System.out.println("key :" + key + ", value :" + value);
        }
    
        // 세번째 : entrySet().iterator()
        Iterator<Map.Entry<String, String>> iteratorE = map.entrySet().iterator();
        while (iteratorE.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iteratorE.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key :" + key + ", value :" + value);
        }
    
        // 네번째 : keySet().iterator()
        Iterator<String> iteratorK = map.keySet().iterator();
        while (iteratorK.hasNext()) {
            String key = iteratorK.next();
            String value = map.get(key);
            System.out.println("key :" + key + ", value :" + value);
        }
\
반응형

 

반응형