Deep_Dev
article thumbnail

๐Ÿ’กHashMap ์ด๋ž€?

HashMap์€ Map ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๋Œ€ํ‘œ์ ์ธ Map ์ปฌ๋ ‰์…˜์ด๋‹ค. Map ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  ์žˆ๊ธฐ์— Map์˜ ์„ฑ์งˆ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ๋‹ค. Map์€ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ Entry๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ํ‚ค์™€ ๊ฐ’์„ ๋ชจ๋‘ ๊ฐ์ฒด์ด๋‹ค.

๊ฐ’์€ ์ค‘๋ณต ์ €์žฅ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ‚ค๋Š” ์ค‘๋ณต์ €์žฅ ๋  ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ๊ธฐ์กด์— ์ €์žฅ๋œ ํ‚ค์™€ ๋™์ผํ•œ ํ‚ค๋กœ ์ €์žฅํ•˜๋ฉด ๊ธฐ์กด์˜ ๊ฐ’์€ ์—†์–ด์ง€๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋Œ€์น˜๋œ๋‹ค. HashMap์€ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•ด์‹ฑ(Hashing)์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ์žˆ์–ด์„œ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๊ฐ€์ง„๋‹ค.

 

์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด HashMap์€ ๋‚ด๋ถ€์— 'ํ‚ค'์™€ '๊ฐ’'์„ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค. HashMap์€ ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด 'ํ‚ค'์™€'๊ฐ’์ด ์ €์žฅ๋˜๋Š” ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•˜๋ฏ€๋กœ, ์‚ฌ์šฉ์ž๋Š” ๊ทธ ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์—†๊ณ , ์‚ฝ์ž…๋˜๋Š” ์ˆœ์„œ์™€ ๋“ค์–ด ์žˆ๋Š” ์œ„์น˜ ๋˜ํ•œ ๊ด€๊ณ„๊ฐ€ ์—†๋‹ค.

 


 

๐Ÿ’กHashMap ์‚ฌ์šฉ๋ฒ•

HashMap ์„ ์–ธ

HashMap<String,String> map1 = new HashMap<String,String>();//HashMap์ƒ์„ฑ
HashMap<String,String> map2 = new HashMap<>();//new์—์„œ ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒ๋žต๊ฐ€๋Šฅ
HashMap<String,String> map3 = new HashMap<>(map1);//map1์˜ ๋ชจ๋“  ๊ฐ’์„ ๊ฐ€์ง„ HashMap์ƒ์„ฑ
HashMap<String,String> map4 = new HashMap<>(10);//์ดˆ๊ธฐ ์šฉ๋Ÿ‰(capacity)์ง€์ •
HashMap<String,String> map5 = new HashMap<>(10, 0.7f);//์ดˆ๊ธฐ capacity,load factor์ง€์ •
HashMap<String,String> map6 = new HashMap<String,String>(){{//์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
    put("a","b");
}};

HashMap์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด Keyํƒ€์ž…๊ณผ Value ํƒ€์ž…์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฃผ๊ณ  ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. HashMap์€ ์ €์žฅ๊ณต๊ฐ„๋ณด๋‹ค ๊ฐ’์ด ์ถ”๊ฐ€๋กœ ๋“ค์–ด์˜ค๋ฉด List์ฒ˜๋Ÿผ ์ €์žฅ๊ณต๊ฐ„์„ ์ถ”๊ฐ€๋กœ ๋Š˜๋ฆฌ๋Š”๋ฐ, List์ฒ˜๋Ÿผ ์ €์žฅ๊ณต๊ฐ„์„ ํ•œ์นธ์”ฉ ๋Š˜๋ฆฌ์ง€ ์•Š๊ณ  ์•ฝ ๋‘๋ฐฐ๋กœ ๋Š˜๋ฆฐ๋‹ค. ( ์—ฌ๊ธฐ์„œ ๊ณผ๋ถ€ํ•˜ ๋งŽ์ด ๋ฐœ์ƒ )

๊ทธ๋ ‡๊ธฐ์— ์ดˆ๊ธฐ์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด Map์˜ ์ดˆ๊ธฐ ์šฉ๋Ÿ‰์„ ์ €์žฅํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 

 

๐Ÿ’กHashMap ๊ฐ’ ์ถ”๊ฐ€

HashMap<Integer, String> map=new HashMap<>(); // new์—์„œ ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒ๋žต๊ฐ€๋Šฅ
map.put(1,"์‚ฌ๊ณผ"); // ๊ฐ’ ์ถ”๊ฐ€
map.put(2,"๋ฐ”๋‚˜๋‚˜");
map.put(3,"ํฌ๋„");

HashMap์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด put(key, value)๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์„ ์–ธ์‹œ HashMap์— ์„ค์ •ํ•ด์ค€ ํƒ€์ž…๊ณผ ๊ฐ™์€ ํƒ€์ž…์˜ Key์™€ Value๊ฐ’์„ ๋„ฃ์–ด์•ผ ํ•˜๋ฉฐ ๋งŒ์•ฝ ์ž…๋ ฅ๋˜๋Š” ํ‚ค ๊ฐ’์ด HashMap ๋‚ด๋ถ€์— ์กด์žฌํ•œ๋‹ค๋ฉด ๊ธฐ์กด์˜ ๊ฐ’์€ ์ƒˆ๋กœ ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์œผ๋กœ ๋Œ€์น˜๋œ๋‹ค.

 

๐Ÿ’กHashMap ๊ฐ’ ์‚ญ์ œ

HashMap<Integer, String> map=new HashMap<Integer,String>(){{ // ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
	put(1,"์‚ฌ๊ณผ");
    put(2,"๋ฐ”๋‚˜๋‚˜");
    put(3,"ํฌ๋„");
}}
map.remove(1) // key๊ฐ’ 1 ์ œ๊ฑฐ
map.clear() // ๋ชจ๋“  ๊ฐ’ ์ œ๊ฑฐ

HashMap์— ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด remove(key) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์˜ค์ง ํ‚ค ๊ฐ’์œผ๋กœ๋งŒ Map์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด clear() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿ’กHashMap ๊ฐ’ ์ถœ๋ ฅ

ashMap<Integer, String> map=new HashMap<Integer,String>(){{ // ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
	put(1,"์‚ฌ๊ณผ");
    put(2,"๋ฐ”๋‚˜๋‚˜");
    put(3,"ํฌ๋„");
}};
System.out.println(map); // ์ „์ฒด ์ถœ๋ ฅ : {1=์‚ฌ๊ณผ, 2=๋ฐ”๋‚˜๋‚˜, 3=ํฌ๋„}
System.out.println(map.get(1)); // key๊ฐ’ 1์˜ value ์–ป๊ธฐ : ์‚ฌ๊ณผ

// entrySet() ํ™œ์šฉ
for(Entry<Integer, String> entry : map.entrySet()){
	System.out.println("[Key:" + entry.getKey() +"[Value]:"+entry.getValue());
}
//[Key]:1 [Value]:์‚ฌ๊ณผ
//[Key]:1 [Value]:๋ฐ”๋‚˜๋‚˜
//[Key]:1 [Value]:ํฌ๋„