[Java] HashMap | Hashtable

Jeiniยท2022๋…„ 12์›” 8์ผ
1

โ˜•๏ธย  Java

๋ชฉ๋ก ๋ณด๊ธฐ
43/59
post-thumbnail

๐Ÿ’ก HashMap


โœ”๏ธ Map ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๋Œ€ํ‘œ์ ์ธ Collection ํด๋ž˜์Šค.
โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
โœ”๏ธ HashMap(๋™๊ธฐํ™” X)์€ Hashtable(๋™๊ธฐํ™” O)์˜ ์‹ ๋ฒ„์ „์ด๋‹ค.

์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋ ค๋ฉด, LinkedHashMap ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

public class HashMap extends AbstractMap implements Map, Cloneable, Serializable {
	transient Entry[] table;
    ...
    static class Entry implements Map.Entry {
    	final Object key;
        Object value;
        ...
    }
}

โ—๏ธ Map.Entry ๋Š” Map์ธํ„ฐํŽ˜์ด์Šค์— ์ •์˜๋œ static inner interface์ด๋‹ค.
โ—๏ธ Map.Entry ๋Š” ํ‚ค์™€ ๊ฐ’์„ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์— ํ•จ๊ป˜ ์ €์žฅํ•˜๋ฏ€๋กœ ๋‘˜ ๋‹ค ๋‹จ์ผ ์ž‘์—…์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

๐Ÿ“Ž HashMap์˜ ํ‚ค(key)์™€ ๊ฐ’(value)

  • ํ•ด์‹ฑ(hashing)๊ธฐ๋ฒ•์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„๋„ ๊ฒ€์ƒ‰์ด ๋น ๋ฅด๋‹ค.

    โœ”๏ธ ํ‚ค(key): ์ปฌ๋ ‰์…˜ ๋‚ด์˜ key ์ค‘์—์„œ ์œ ์ผํ•ด์•ผ ํ•œ๋‹ค.
    โœ”๏ธ ๊ฐ’(value): key์™€ ๋‹ฌ๋ฆฌ ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ๋‹ค.

HashMap map = new HashMap();

map.put("myId", "1234");
map.put("asdf", "1111");
map.put("asdf", "1234");

๐Ÿ“Ž ํ•ด์‹ฑ(hasing)

โœ”๏ธ ํ‚ค ๊ฐ’์„ ๋„ฃ์œผ๋ฉด index(์ €์žฅ ๊ณต๊ฐ„)๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

โœ”๏ธ ํ•ด์‹œํ•จ์ˆ˜(hash function)๋กœ HashMap ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ, ๊ฒ€์ƒ‰ํ•œ๋‹ค.

โœ”๏ธ HashMap ์€ ๋ฐฐ์—ด๊ณผ LinkedList ๊ฐ€ ์กฐํ•ฉ๋œ ํ˜•ํƒœ์ด๋‹ค.

๋ณ€๊ฒฝํ•˜๊ธฐ ์‰ฝ๊ณ  ์ธ๋ฑ์Šค๋งŒ ์•Œ๋ฉด ํ•œ๋ฒˆ์— ์ฐพ์•„๊ฐ€๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด๊ณผ LinkedList๋ฅผ ๊ฐ€์ง€๊ณ  ์™”๋‹ค.

๐Ÿ“Ž HashMap` ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ณผ์ •


1. key๋กœ ํ•ด์‰ฌํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ์–ป๋Š”๋‹ค.
2. ํ•ด์‹œ์ฝ”๋“œ(ํ•ด์‹œํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’)์— ๋Œ€์‘ํ•˜๋Š” LinkedList๋ฅผ ๋ฐฐ์—ด์—์„œ ์ฐพ๋Š”๋‹ค.
3. LinkedList์—์„œ key์™€ ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š”๋‹ค.

โ—๏ธ ํ•ด์‹œํ•จ์ˆ˜๋Š” ๊ฐ™์€ ํ‚ค์— ๋Œ€ํ•ด ํ•ญ์ƒ ๊ฐ™์€ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
์„œ๋กœ ๋‹ค๋ฅธ ํ‚ค์ผ์ง€๋ผ๋„ ๊ฐ™์€ ๊ฐ’์˜ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

โœ๏ธ HashMap์˜ ์ƒ์„ฑ์ž

โ—๏ธ load factor?

  • ์ €์žฅ๊ณต๊ฐ„์ด ๊ฐ€๋“ ์ฐจ๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ์šฉ๋Ÿ‰์„ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ
  • ์ €์žฅ๊ณต๊ฐ„์ด 70%๊ฐ€ ์ฑ„์›Œ์กŒ์„ ๋•Œ ์šฉ๋Ÿ‰์ด 2๋ฐฐ๋กœ ๋Š˜์–ด๋‚œ๋‹ค.
  • ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ๋„ ๋””ํดํŠธ ๊ฐ’์€ 75%๋กœ load Factor๊ฐ€ 0.75์ด๋‹ค.

โœ๏ธ HashMap์˜ ๋ฉ”์„œ๋“œ


โœ๏ธ id / password ์ผ์น˜ ์˜ˆ์ œ

import java.util.*;

public class HashMapEx2 {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("jipark09", "0922");
        map.put("park", "1111");
        map.put("smpark07", "0725");

        System.out.println(map);

        Scanner sc = new Scanner(System.in);

        while(true) {
            System.out.println("id์™€ password๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
            
            System.out.println("id: ");
            String id = sc.nextLine().trim();

            System.out.println("password: ");
            String password = sc.nextLine().trim();
            
            System.out.println();

            if(!map.containsKey(id)) {
                System.out.println("์ž…๋ ฅํ•˜์‹  id๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
                continue;
            }
            if(!(map.get(id)).equals(password)) {
                System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
                continue;
            } else {
                System.out.println("ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!");
                break;
            }
        }
    }
}

โœ๏ธ ํ•™์ƒ์˜ ๋ช…๋‹จ๊ณผ ์ด์  / ํ‰๊ท  / ์ตœ๊ณ ์ ์ˆ˜ / ์ตœ๊ณ ์ ์ˆ˜

import java.util.*;

public class HashMapEx3 {
    public static void main(String[] args) {
        HashMap map = new HashMap();

        map.put("๊น€์ž๋ฐ”", new Integer(90));
        map.put("๊น€์ž๋ฐ”", new Integer(100));
        map.put("์ด์ž๋ฐ”", new Integer(100));
        map.put("๊ฐ•์ž๋ฐ”", new Integer(80));
        map.put("๋ฐ•์ž๋ฐ”", new Integer(90));

        Set set = map.entrySet(); // iterator()๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•ด
        Iterator it = set.iterator();

        while(it.hasNext()) {
            // Entry ํด๋ž˜์Šค๋Š” ํ‚ค์™€ ๊ฐ’์„ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์— ํ•จ๊ป˜ ์ €์žฅํ•˜๋ฏ€๋กœ ๋™์‹œ์— ํ‚ค์™€ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
            Map.Entry e = (Map.Entry)it.next();
            System.out.println("์ด๋ฆ„: " + e.getKey() + " [์ ์ˆ˜: " + e.getValue() + "]");
        }
        set = map.keySet();
        System.out.println("์ฐธ๊ฐ€์ž ๋ช…๋‹จ: " + set);

        Collection values = map.values();
        it = values.iterator();

        int total = 0;

        while(it.hasNext()) {
            int i = (int)it.next();
            total += i;
        }
        System.out.println("์ด์ : " + total);
        System.out.println("ํ‰๊ท : " + (double)total / set.size());
        System.out.println("์ตœ๊ณ ์ ์ˆ˜: " + Collections.max(values));
        System.out.println("์ตœ์ €์ ์ˆ˜: " + Collections.min(values));


    }
}
์ด๋ฆ„: ๊น€์ž๋ฐ” [์ ์ˆ˜: 100]
์ด๋ฆ„: ๋ฐ•์ž๋ฐ” [์ ์ˆ˜: 90]
์ด๋ฆ„: ๊ฐ•์ž๋ฐ” [์ ์ˆ˜: 80]
์ด๋ฆ„: ์ด์ž๋ฐ” [์ ์ˆ˜: 100]
์ฐธ๊ฐ€์ž ๋ช…๋‹จ: [๊น€์ž๋ฐ”, ๋ฐ•์ž๋ฐ”, ๊ฐ•์ž๋ฐ”, ์ด์ž๋ฐ”]
์ด์ : 370
ํ‰๊ท : 92.5
์ตœ๊ณ ์ ์ˆ˜: 100
์ตœ์ €์ ์ˆ˜: 80

๐Ÿ’ก TreeMap


  • ๋ฒ”์œ„ ๊ฒ€์ƒ‰๊ณผ ์ •๋ ฌ์— ์œ ๋ฆฌํ•œ Collection ํด๋ž˜์Šค.
  • HashMap๋ณด๋‹ค ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€, ์‚ญ์ œ์— ์‹œ๊ฐ„์ด ๋” ๊ฑธ๋ฆฐ๋‹ค. (๋น„๊ต ์ €์žฅ)
  • TreeSet์€ TreeMap์œผ๋กœ ๋งŒ๋“ค์œผ๋กœ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ํŠน์„ฑ์„ ๊ฐ€์ง„๋‹ค.

References
: https://cafe.naver.com/javachobostudy
: https://staticclass.tistory.com/108 (์ƒ์„ฑ์ž / ๋ฉ”์„œ๋“œ ์‚ฌ์ง„ ์ถœ์ฒ˜)

profile
Fill in my own colorful colors๐ŸŽจ

0๊ฐœ์˜ ๋Œ“๊ธ€