map2 Map 순회하기 Iterator 사용 Map map = new HashMap(); Iterator keys = map.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); map.get(key); } entrySet 사용 Map map = new HashMap(); for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); } Map은 Key-Value 쌍으로 이루어져있고, 이 한 묶음을 Entry라고 표현한다. keySet 사용 Map map = new HashMap(); for (String key : map.keySe.. 2023. 2. 10. Map 인터페이스를 구현한 클래스에서 Iterator 사용하기 모든 Collection 인터페이스를 구현하는 클래스(ArrayList, HashSet, TreeSet...)들은 iterator() 메서드를 사용할 수 있다. 그러나, Map 인터페이스를 구현하는 클래스(HashMap, TreeMap...)들은 사용이 불가하다. 그래서, 다음 예시 코드처럼 Set의 형태로 바꾼 후, iterator() 메서드들을 사용하도록 한다. 자료 구조의 체계를 다시 확인해보고 싶다면 https://jaeyoungb.tistory.com/103를 참고하자. 다음 예시 코드를 참고해서 사용해보자. 2022. 9. 18. 이전 1 다음