JAVA

[Skill] JAVA 11 에서 Map 컨테이너 순회하는 방법

신우섭 2020. 9. 8. 17:47

예시

1. forEach + 람다함수 사용

Map<String, Stirng> tempMap = new HashMap<String, String>;
tempMap.put("가", "A");
tempMap.put("나", "B");
tempMap.put("다", "C");

tempMap.forEach((key, value)->{ 
    System.out.println(key); // key
    System.out.println(value); // value
});

2. 향상된 For문 + Set 사용

Map<String, Stirng> tempMap = new HashMap<String, String>;
tempMap.put("가", "A");
tempMap.put("나", "B");
tempMap.put("다", "C");

for (String key : tempMap.KeySet()) {  
    System.out.println(key); // key  
    System.out.println(tempMap.get(key)); // value  
}