Collection - Map

Map Collection์€ ํ‚ค(key)์™€ ๊ฐ’(value)๋กœ ๊ตฌ์„ฑ๋œ Entry ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ key์™€ value๋Š” ๋ชจ๋‘ ๊ฐ์ฒด์ด๋‹ค. key๋Š” ์ค‘๋ณต ์ €์žฅ๋  ์ˆ˜ ์—†์ง€๋งŒ value๋Š” ์ค‘๋ณต ์ €์žฅ๋  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ๊ธฐ์กด์— ์ €์žฅ๋œ key์™€ ๋™์ผํ•œ key๋กœ value์„ ์ €์žฅํ•˜๋ฉด ๊ธฐ์กด์˜ value์€ ์—†์–ด์ง€๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋Œ€์น˜๋œ๋‹ค.

๊ธฐ๋Šฅ

๋ฉ”์†Œ๋“œ

์„ค๋ช…

๊ฐ์ฒด ์ถ”๊ฐ€

V put(K key,V value)

์ฃผ์–ด์ง„ ํ‚ค์™€ ๊ฐ’์„ ์ถ”๊ฐ€, ์ €์žฅ๋˜๋ฉด ๊ฐ’์„ return

๊ฐ์ฒด ๊ฒ€์ƒ‰

boolean containsKey(Object key)

์ฃผ์–ด์ง„ ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€

boolean containsValue(Object value)

์ฃผ์–ด์ง„ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€

Set> entrySet()

ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ชจ๋“  Map.Entry ๊ฐ์ฒด๋ฅผ Set์— ๋‹ด์•„์„œ return

V get(Object key)

์ฃผ์–ด์ง„ ํ‚ค๊ฐ€ ์žˆ๋Š” ๊ฐ’์„ ๋ฆฌํ„ด

boolean isEmpty()

Collection์ด ๋น„์–ด์žˆ๋Š”์ง€ ์—ฌ๋ถ€

Set<K> keySet()

๋ชจ๋“  ํ‚ค๋ฅผ Set ๊ฐ์ฒด์— ๋‹ด์•„์„œ return

int size()

์ €์žฅ๋œ ํ‚ค์˜ ์ด ์ˆ˜๋ฅผ return

Collection<V> values()

์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ’์„ Collection์— ๋‹ด์•„์„œ return

๊ฐ์ฒด ์‚ญ์ œ

void clear()

๋ชจ๋“  Map.Entry(ํ‚ค์™€ ๊ฐ’) ์‚ญ์ œ

V remove(Object key)

์ฃผ์–ด์ง„ ํ‚ค์™€ ์ผ์น˜ํ•˜๋Š” Map.Entry ์‚ญ์ œํ•˜๊ณ  ๊ฐ’์„ return

Map<String, Integer> map = ~;
map.put("์ •๋‹คํ˜œ",24);
int score = map.get("์ •๋‹คํ˜œ");
map.remove("์ •๋‹คํ˜œ");

key๋ฅผ ์•Œ๊ณ  ์žˆ์œผ๋ฉด get()์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ฐพ์•„์˜ค๋ฉด๋˜์ง€๋งŒ, ์ €์žฅ๋œ ์ „์ฒด ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋‚˜์”ฉ ์–ป๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

Map<K,V> map = ~;
Set<K> keySet = map.keySet();
Iterator<K> keyIterator = keySet.iterator();
while(keyIterator.hasNext()){
    K key = keyIterator.next();
    V value = map.get(key);
}
Set<Map.Entry<K,V>> entrySet = map.entrySet();
Iterator<Map.Entry<K,V>> entryIterator = entrySet.iterator();
while(entrySet.hasNext()){
    Map.Entry<K,V> entry = entryIterator.next();
    K key = entry.getKey();
    V value = entry.getValue();
}

HashMap

HashMap์€ ์—ฐ๊ด€ ๋ฐฐ์—ด(Key-Value)์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ์ด๋‹ค. ํ‚ค๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๊ฐ’์„ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

HashMap์˜ ํ‚ค๋กœ ์‚ฌ์šฉ๋  ๊ฐ์ฒด๋Š” hashCode()์™€ equals() ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์„œ ๋™๋“ฑ ๊ฐ์ฒด๊ฐ€ ๋  ์กฐ๊ฑด์„ ์ •ํ•ด์•ผํ•œ๋‹ค. ๋™์ผํ•œ ํ‚ค๊ฐ€ ๋  ์กฐ๊ฑด์€ hashCode()์˜ return๊ฐ’์ด ๊ฐ™์•„์•ผํ•˜๊ณ , equals() ๋ฉ”์†Œ๋“œ๊ฐ€ true๋ฅผ returnํ•ด์•ผํ•œ๋‹ค.

Map<K,V> map = new HashMap<K, V>();

ํ‚ค์™€ ๊ฐ’์˜ ํƒ€์ž…์€ ๊ธฐ๋ณธํƒ€์ž…(byte, short, int, float, double, boolean, char)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ , ํด๋ž˜์Šค ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

Map<String,Integer> map = new HashMap<String, Integer>();
import java.util.*;

public class HashMapEx{
    public static void main(String[] args){
        //Map Collection
        Map<String, Integer> map = new HashMap<String, Integer>();

        // ๊ฐ์ฒด์ €์žฅ
        map.put("์ •๋‹คํ˜œ",100);
        map.put("์ •๋ฏธ๋ž˜",99);
        map.put("๊น€ํ•˜์˜",98);

        // ๊ฐ์ฒด ์ฐพ๊ธฐ
        System.out.println(map.get("๊น€ํ•˜์˜"));

        // ๊ฐ์ฒด ํ•œ๊ฐœ์”ฉ ์ฒ˜๋ฆฌ
        Set<String> keySet = map.keySet();
        Iterator<String> keyIterator = keySet.iterator();
        while(keyIterator.hasNext()){
            String key = keyIterator.next();
            Integer value = map.get(key);
        }

        // ๊ฐ์ฒด ์‚ญ์ œ
        map.remove("์ •๋‹คํ˜œ");
    }
}

HashMap.keySet() ์„ ํ†ตํ•˜์—ฌ Set์„ ๊บผ๋‚ด๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๋ฐ˜ํ™˜๋˜๋Š” Set์˜ ๋™์ž‘์—์„œ HashMap์˜ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์˜ ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

class Student{
    public int sno;
    public String name; 

    public Student(int sno, String name){
        this.sno = sno;
        this.name = name;
    }

    public boolean equals(Object obj){
        if(obj instanceof Student){
            Student student = (Student)obj;
            return (sno==student.sno) && (name.equals(student.name));
        }else{
            return false;
        }
    }

    public int hashCode(){
        return sno + name.hashCode();
    }
}
public class HashMapEx{
    public static void main(String[] args){
        Map<Student, Integer> map = new HashMap<Student,Integer>();

        // ๋™์ผํ•œ key๋ฅผ ์ €์žฅํ–ˆ์œผ๋ฏ€๋กœ map.size()๋Š” 1์ด๋‹ค.
        map.put(new Student(1,"์ •๋‹คํ˜œ"),95);
        map.put(new Student(1,"์ •๋‹คํ˜œ"),95);
    }
}

sychronizedMap

Collection ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๋Œ€๋ถ€๋ถ„์˜ ํด๋ž˜์Šค(HashMap)๋Š” Single Thread ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์•ˆ์ „ํ•˜์ง€์•Š๋‹ค. Single Threadํ™˜๊ฒฝ์—์„œ Multi Threadํ™˜๊ฒฝ์œผ๋กœ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ์— ๋Œ€๋น„ํ•ด ๋น„๋™๊ธฐํ™”๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ๋™๊ธฐํ™”๋œ ๋ฉ”์†Œ๋“œ๋กœ Wrappingํ•˜๋Š” synchronizedMap()๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

Map

synchronizedMap(Map m)

Map์„ ๋™๊ธฐํ™”๋œ Map๋กœ ๋ฆฌํ„ด

Map<K,V> map = Collections.synchronizedMap(new HashMap<K,V>());

ConcurrentHashMap

๋™๊ธฐํ™”๋œ ์ปฌ๋ Œ์…˜์€ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์š”์†Œ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋„์™€์ฃผ์ง€๋งŒ, ์ „์ฒด ์š”์†Œ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜์ง€๋Š” ๋ชปํ•œ๋‹ค. ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ• ๋•Œ ์ „์ฒด ์ž ๊ธˆ์ด ๋ฐœ์ƒํ•ด ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋Š” ๋Œ€๊ธฐ์ƒํƒœ๊ฐ€๋œ๋‹ค. ์ž๋ฐ”๋Š” ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๊ฐ€ ์ปฌ๋ ‰์…˜์˜ ์š”์†Œ๋ฅผ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปฌ๋ ‰์…˜์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

Map<K,V> map = new ConcurrentHashMap<K,V>();

ConcurrentHashMap์€ ๋ถ€๋ถ„(segment) ์ž ๊ธˆ์„ ์‚ฌ์šฉํ•ด, ์ฒ˜๋ฆฌํ•˜๋Š” ์š”์†Œ๊ฐ€ ํฌํ•จ๋œ segment๋งŒ ์ž ๊ธˆํ•˜๊ณ , ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

LinkedHashMap

LinkedHashMap์€ HashMap์„ ํ™•์žฅํ•˜๋Š” ํด๋ž˜์Šค๋กœ ์ž๋ฃŒ๊ฐ€ ์ž…๋ ฅ๋œ ์ˆœ์„œ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

  • key-value ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ

  • ์ „์ฒดํฌ๊ธฐ๋Š” ์•Œ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ

  • ์ˆœ์„œ๋ฅผ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์— ๊ฐ„ํŽธํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ์ฆ‰, HashMap์„ ํ†ตํ•ด ์ž๋ฃŒ๋ฅผ ๋ณด๊ด€ํ•  ํ•„์š”๊ฐ€ ์žˆ์ง€๋งŒ, ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€์ ธ์™€์•ผํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค. HashMap์„ ํ™•์žฅํ–ˆ์œผ๋ฏ€๋กœ, HashMap์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Doubly-Linked List๋ฅผ ๋‚ด๋ถ€์— ์œ ์ง€ํ•จ์œผ๋กœ์จ ์ž…๋ ฅ๋œ ์ž๋ฃŒ ์ˆœ์„œ๋ฅผ ๊ธฐ์–ตํ•  ์ˆ˜ ์žˆ๋‹ค.

LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("map", "LinkedHashMap");
map.put("array", "ArrayList");

map.get("map"); // "LinkedHashMap"
map.get("array"); // "ArrayList"

map.forEach((key, value) -> {
    System.out.println(key + " : " + value);
});

Hashtable

HashMap๊ณผ์˜ ์ฐจ์ด์ ์€ Hashtable์€ ๋™๊ธฐํ™”๋œ(synchronized) ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์ด ๋ฉ”์†Œ๋“œ๋“ค์„ ์‹คํ–‰ํ•  ์ˆ˜ ์—†๊ณ , ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰์„ ์™„๋ฃŒํ•ด์•ผ๋งŒ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

Map<K,V> map = new Hashtable<K,V>();

Properties

Properties๋Š” Hashtable์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— Hashtable์˜ ๋ชจ๋“  ํŠน์ง•์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ฐจ์ด์ ์€ Hashtable์€ ํ‚ค์™€ ๊ฐ’์„ ๋‹ค์–‘ํ•œ ํƒ€์ž…์œผ๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•œ๋ฐ ๋น„ํ•ด Properties๋Š” ํ‚ค์™€ ๊ฐ’์„ String ํƒ€์ž…์œผ๋กœ ์ œํ•œํ•œ Collection์ด๋‹ค.

Properties๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์˜ต์…˜์ •๋ณด, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด, ๊ตญ์ œํ™” ์ •๋ณด๊ฐ€ ์ €์žฅ๋œ (~.properties)ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

driver = oracle.jdbc.OracleDriver
url = jdbc:oracle:thin:@localhost:1521:orcl
username=name
password=password
Properties properties = new Properties();
// ๋ฐ์ดํ„ฐ ์ฝ์–ด์˜ค๊ธฐ
properties.load(new FileReader("properties ํŒŒ์ผ๊ฒฝ๋กœ"));

์ฐธ๊ณ 

Last updated