Map Collection이란? :: 행복한 프로그래머

posted by 쁘로그램어 2018. 5. 23. 04:01

Map 컬렉션은 키(key)와 값(value)으로 구성된 Entry 객체를 저장하는 구조를 가지고 있습니다.

여기서 키와 값은 모두 객체입니다. 키는 중복될 수 없지만 값은 중복 저장될 수 있습니다.

만약 기존에 저장된 키와 동일한 키로 값을 저장하면 기존의 값은 없어지고 새로운 값으로 대치됩니다.


# HashMap

* Map 인터페이스를 구현하기 위해 해시테이블을 사용한 클래스

* 중복을 허용하지 않고 순서를 보장하지 않음

* 키와 값으로 null이 허용


# Hashtable

* HashMap 보다는 느리지만 동기화가 지원 (즉 멀티스레드에 안전)

* 키와 값으로 null이 허용되지 않음


# TreeMap

* 이진검색트리의 형태로 키와 값의 쌍으로 이루어진 데이터를 저장

* 정렬된 순서로 키/값 쌍을 저장하므로 빠른 검색이 가능

* 저장시 정렬(오름차순)을 하기 때문에 저장시간이 다소 오래 걸림


# LinkedHashMap

* 기본적으로 HashMap을 상속받아 HashMap과 매우 흡사

* Map에 있는 엔트리들의 연결 리스트를 유지되므로 입력한 순서대로 반복 가능


# ExHashMap.java

public class ExHashMap {

 

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<String, Integer>();

        

        map.put("Jack", 30);

        map.put("Andy", 40);

        map.put("John", 22);

        map.put("Jolie", 10);

        map.put("Exo", 50);

        map.put("Tiger", 91);

        

        System.out.println("총 Entry 수: " + map.size());

        

        System.out.println("\tJolie" + map.get("Jolie"));

        System.out.println();

        

        // 객체를 하나씩 처리

        Set<String> keySet = map.keySet();

        Iterator<String> it = keySet.iterator();

        

        while (it.hasNext()) {

            String key = it.next();

            Integer value = map.get(key);

            System.out.println("\t" + key + " : " + value);

        }

        

        System.out.println();

        

        // 객체 삭제

        map.remove("Exo");

        System.out.println("총 Entry 수: " + map.size());

        

        map.clear();

        System.out.println("총 Entry 수: " + map.size());

    }

}


# 결과

총 Entry 수: 6

Jolie10


Jolie : 10

John : 22

Jack : 30

Andy : 40

Exo : 50

Tiger : 91


총 Entry 수: 5

총 Entry 수: 0 


※ 참고 사이트 ※

http://palpit.tistory.com/656

http://stove99.tistory.com/96

http://hackersstudy.tistory.com/26

http://arabiannight.tistory.com/entry/%EC%9E%90%EB%B0%94Java-%EC%9E%90%EB%B0%94-HashMap-%EC%9D%B4%EB%9E%80

'Java > Java' 카테고리의 다른 글

타이머: Timer, ScheduledExcutorService  (0) 2018.06.19
GSON – How to parse input JSON with dynamic keys  (0) 2018.05.28
Set Collection이란?  (0) 2018.05.23
List Collection이란?  (0) 2018.05.23
컬렉션 프레임워크(Collection Framework)란?  (0) 2018.05.23