본문 바로가기
java/컬렉션 프레임웍

Map

by doni73 2025. 4. 17.

Map

(중복 x) + (중복 o)으로 구성
하나의 키 + 값을 entry라고 부름
순서 x

HashMap

HashTable 신버전
HashMap은 동기화 x, HashTable은 동기화 o

import java.util.*;
class HashMapEx3 {
    static HashMap phoneBook = new HashMap();
    public static void main(String[] args) {
        addPhoneNo("친구", "엄준식", "010-0000-0000");
        addPhoneNo("친구", "어쩌구", "010-9999-0000");
        addPhoneNo("친구", "저쩌구", "010-9999-5555");
        addPhoneNo("회사", "사장님", "010-1234-6643");
        addPhoneNo("회사", "실장님", "010-5463-2574");
        addPhoneNo("회사", "팀장님", "010-1111-2222");
        addPhoneNo("누구세요", "010-3333-5555");
        addPhoneNo("누구", "010-7777-5555");
        addPhoneNo("누구?", "010-4444-5555");

        printPhoneBook();
    }
    static void addPhoneNo(String groupName, String name, String tel) {
        addGroup(groupName); //addGroup()에서 중복 검사 함
        HashMap group = (HashMap) phoneBook.get(groupName); //해당 그룹의 HashMap 가져옴
        group.put(tel, name);
    }
    static void addGroup(String groupName) {
        if(!phoneBook.containsKey(groupName)) { //그룹 중복 체크
            phoneBook.put(groupName, new HashMap());
        }
    }
    static void addPhoneNo(String name, String tel) {
        addPhoneNo("기타", name, tel); //그룹 지정 안 했을 경우에 기타로 저장
    }

    static void printPhoneBook() {
        Set set = phoneBook.entrySet();
        Iterator it = set.iterator();

        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next(); 
            Set groupSet = ((HashMap) entry.getValue()).entrySet(); //해당 그룹의 HashMap가져오기
            Iterator groupIt = groupSet.iterator();
            System.out.println("===" + entry.getKey() +"(" +groupSet.size() + ")"+ "==="); //그룹
            while(groupIt.hasNext()) {
                Map.Entry gEntry = (Map.Entry)groupIt.next(); 
                System.out.println(gEntry.getValue() + " " + gEntry.getKey());
            }
            System.out.println();
        }
    }
}
===기타(3)===
누구 010-7777-5555
누구세요 010-3333-5555
누구? 010-4444-5555

===친구(3)===
엄준식 010-0000-0000
저쩌구 010-9999-5555
어쩌구 010-9999-0000

===회사(3)===
팀장님 010-1111-2222
사장님 010-1234-6643
실장님 010-5463-2574

-> PhoneBook의 entry -> key(그룹명) + value(해당 그룹의 연락처 HashMap)
해당그룹의 연락처 HashMap의 entry -> key(연락처) + value(이름)

-> Iterator를 쓰는 이유 : 'iterator()'은 List나 Set에 저장된 요소를 읽어올 수 있어서 Set에 저장 후 사용 가능

 

위 코드중에서

while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next(); 
            Set groupSet = ((HashMap) entry.getValue()).entrySet(); //해당 그룹의 HashMap가져오기
            Iterator groupIt = groupSet.iterator();
            System.out.println("===" + entry.getKey() +"(" +groupSet.size() + ")"+ "==="); //그룹
            while(groupIt.hasNext()) {
                Map.Entry gEntry = (Map.Entry)groupIt.next(); 
                System.out.println(gEntry.getValue() + " " + gEntry.getKey());
            }
            System.out.println();
        }

-> 'it.next()'는 Object 타입으로 반환돼서 형변환이 필요함!

-> 'entrySet()'을 사용하려면 HashMap으로 형변환이 필요함!

HashSet이랑 TreeSet이랑 비슷!

'java > 컬렉션 프레임웍' 카테고리의 다른 글

Comparator와 Comparable  (0) 2025.04.21
Set  (1) 2025.04.17
Iterator, ListIterator, Enumeration  (0) 2025.04.17
컬렉션 프레임웍  (0) 2025.04.15