CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /์ž๋ฐ” ํ•ด์‹œ ํ…Œ์ด๋ธ”
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

์ž๋ฐ” ํ•ด์‹œ ํ…Œ์ด๋ธ”

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
Java Hashtable ํด๋ž˜์Šค๋Š” Java Collection Framework์˜ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ๋ฉค๋ฒ„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ˆ˜ํ•™์  ํ•ด์‹œ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค. Java ํ•ด์‹œ ํ…Œ์ด๋ธ”์—๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ํ‚ค/๊ฐ’ ์Œ์ด ์ €์žฅ๋˜๋Š” ๋ฒ„ํ‚ท์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Hashtable์€ HashMap ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๋“ค ์‚ฌ์ด์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์€ Hashtable์ด ๋™๊ธฐํ™”๋˜๋Š” ๋ฐ˜๋ฉด HashMap์€ ๋™๊ธฐํ™”๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ์„œ์˜ ํ•ด์‹œ ํ…Œ์ด๋ธ”

Hashtable์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐฐ์—ด ํ˜•์‹์œผ๋กœ ์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๊ฐ’์—๋Š” ๊ณ ์œ ํ•œ ํ‚ค ๊ฐ’์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ‚ค๊ฐ€ ์•Œ๋ ค์ง„ ๊ฒฝ์šฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๊ฐ€ ๋งค์šฐ ๋น ๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฝ์ž… ๋ฐ ๊ฒ€์ƒ‰ ์ž‘์—…์€ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ์— ๊ด€๊ณ„์—†์ด ๋น ๋ฆ…๋‹ˆ๋‹ค. Hash Table์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฐ์—ด๊ณผ ์š”์†Œ๊ฐ€ ์œ„์น˜ํ•ด์•ผ ํ•  ์ธ๋ฑ์Šค ์ƒ์„ฑ์„ ์œ„ํ•œ ํ•ด์‹ฑ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ•ด์‹ฑ์ด๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๊ฐ์ฒด๋ฅผ ์ผ๋ จ์˜ ๋ฌธ์ž(์ฝ”๋“œ)๋กœ ๋งคํ•‘ํ•˜๋Š” ๊ทœ์น™์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ์ข…๋ฅ˜์˜ ํ•จ์ˆ˜๋Š” ํฐ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ์„ ์ž‘์€ ์ •์ˆ˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ด์‹œ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชจ๋‘ ํŠน์ • ์†์„ฑ์„ ์ œ์ถœํ•ฉ๋‹ˆ๋‹ค.
  • ํŠน์ • ๊ฐœ์ฒด์—๋Š” ํŠน์ • ํ•ด์‹œ ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‘ ๊ฐœ์˜ ๋™์ผํ•œ ๊ฐœ์ฒด์—๋Š” ๋™์ผํ•œ ํ•ด์‹œ ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋ฐ˜๋Œ€๋Š” ์‚ฌ์‹ค์ด ์•„๋‹™๋‹ˆ๋‹ค.
  • ๋‘ ๊ฐœ์˜ ํ•ด์‹œ ์ฝ”๋“œ๊ฐ€ ๋‹ค๋ฅด๋ฉด ๊ฐœ์ฒด๊ฐ€ ํ™•์‹คํžˆ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋‹ค๋ฅธ ๊ฐœ์ฒด๋Š” ๋™์ผํ•œ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋งค์šฐ ๋“œ๋ฌธ ์ด๋ฒคํŠธ๋Š” ์ถฉ๋Œ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ข‹์€ ํ•ด์‹œ ํ•จ์ˆ˜๋Š” ์ถฉ๋Œ ๊ฐ€๋Šฅ์„ฑ์„ ์ตœ์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค.
Object์— Hash Function์„ ์ ์šฉํ•œ ๊ฒฐ๊ณผ hashCode๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค .

Java์˜ ํ•ด์‹œ ํ…Œ์ด๋ธ”

Hashtable ํด๋ž˜์Šค๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค. ์ด ์ปฌ๋ ‰์…˜์€ Java Collection Framework๋ณด๋‹ค ๋จผ์ € ์ƒ์„ฑ๋˜์—ˆ์ง€๋งŒ ๋‚˜์ค‘์— ํฌํ•จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  "์ดˆ๊ธฐ" ์ปฌ๋ ‰์…˜(Java 1.0๋ถ€ํ„ฐ)๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด์‹œ ํ…Œ์ด๋ธ”์€ ๋™๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค(๊ฑฐ์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๊ฐ€ ๋™๊ธฐํ™”๋จ์œผ๋กœ ํ‘œ์‹œ๋จ). ์ด ์š”์†Œ ๋•Œ๋ฌธ์— ํ•ด์‹œ ํ…Œ์ด๋ธ”์—๋Š” ์‹ฌ๊ฐํ•œ ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Java 1.2๋ถ€ํ„ฐ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋™๊ธฐํ™” ๋ถ€์กฑ์œผ๋กœ ์ธํ•ด Map ์ธํ„ฐํŽ˜์ด์Šค ์˜ ๋‹ค๋ฅธ ๊ตฌํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค . ์ผ๋ฐ˜์ ์œผ๋กœ HashMap์ด ๊ฐ€์žฅ ์ ์ ˆํ•œ ๋Œ€์ฒดํ’ˆ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํด๋ž˜์Šค Hashtable<K,V>ํ‚ค์™€ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ•ด์‹ฑ ์›์น™์— ๋”ฐ๋ผ ํ‚ค๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ‚ค-๊ฐ’ ์Œ์€ "๋ฒ„ํ‚ท"์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฒ„ํ‚ท์€ ์ผ์ข…์˜ ๋‚ด๋ถ€ ๋ฐฐ์—ด์ธ "ํ…Œ์ด๋ธ”"์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. Hashtable์€ ํ‚ค์˜ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค/๊ฐ’ ์Œ์ด ๋งคํ•‘๋˜์–ด์•ผ ํ•˜๋Š” ๋ฒ„ํ‚ท์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ‚ค์˜ ํ•ด์‹œ์ฝ”๋“œ์—์„œ ๋ฒ„ํ‚ท ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ๋งํ–ˆ๋“ฏ์ด ๋‘ ๊ฐœ์˜ ๋™์ผํ•œ ๊ฐœ์ฒด๋Š” ๋™์ผํ•œ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ–๋Š” ๋ฐ˜๋ฉด ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๊ฐœ์ฒด๋Š” ํ•ญ์ƒ ๋‹ค๋ฅธ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ–์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ๋„ฃ์€ ๋‹ค๋ฅธ ๊ฐœ์ฒด๋Š” ๋™์ผํ•œ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ(์ถฉ๋Œ)๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ชฉ๋ก ๋ฐฐ์—ด์ด ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ๋ฒ„ํ‚ท์— ๋งคํ•‘๋œ ์Œ์€ ๋ชฉ๋ก์— ์ €์žฅ๋˜๊ณ  ์ด ๋ชฉ๋ก ์ฐธ์กฐ๋Š” ๋ฐฐ์—ด ์ธ๋ฑ์Šค์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

ํ•ด์‹œ ํ…Œ์ด๋ธ” Java ์ƒ์„ฑ์ž

  • Hashtable() , ๊ธฐ๋ณธ ์ƒ์„ฑ์ž. ๋นˆ ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. (๊ธฐ๋ณธ ์ดˆ๊ธฐ ์šฉ๋Ÿ‰ = 11, ๋ถ€ํ•˜์œจ = 0.75).
  • Hashtable(int size)์€ ์ง€์ •๋œ ํฌ๊ธฐ์˜ ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Hashtable(int size, float fillRatio)์€ ์ง€์ •๋œ ํฌ๊ธฐ์™€ ์ฑ„์šฐ๊ธฐ ๋น„์œจ์˜ ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Hashtable(Map m)์€ ์ฃผ์–ด์ง„ Map๊ณผ ๋™์ผํ•œ ๋งคํ•‘์œผ๋กœ ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

ํ•ด์‹œ ํ…Œ์ด๋ธ” ์„ ์–ธ

Hashtable Java ํด๋ž˜์Šค๋Š” Map , Cloneable ๋ฐ Serializable ์ธํ„ฐํŽ˜์ด์Šค ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๊ฒƒ์€ Dictionary ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.

Hashtable.java
public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable
K ๋Š” ๋งต์—์„œ ์œ ์ง€ ๊ด€๋ฆฌํ•˜๋Š” ํ‚ค ์œ ํ˜•์ž…๋‹ˆ๋‹ค. V ๋Š” ๋งคํ•‘๋œ ๊ฐ’์˜ ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ์˜ˆ:

Hashtable<Student, Integer> myHTable = new Hashtable<>();

ํ•ด์‹œ ํ…Œ์ด๋ธ” ์ž๋ฐ”๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•

Java Hashtable์€ java.util ํŒจํ‚ค์ง€ ์•ˆ์— ์žˆ์Šต๋‹ˆ๋‹ค . ๋”ฐ๋ผ์„œ import java.util.Hashtable์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. ๊ท€ํ•˜์˜ ์ฝ”๋“œ์—์„œ. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด์— ๋Œ€ํ•œ ํžŒํŠธ๋Š” IDE์—์„œ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•ด์‹œ ํ…Œ์ด๋ธ” ์ฃผ์š” ์ž‘์—…

Hashtable์˜ ์ฃผ์š” ์ž‘์—…์€ ๊ฐ€์ ธ์˜ค๊ธฐ, ์ปฌ๋ ‰์…˜์— ์‚ฝ์ž… ๋ฐ ์ œ๊ฑฐ์ž…๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐ€์ง€ ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ๊ฐ์ฒด get(๊ฐ์ฒด ํ‚ค)์€ ํ‚ค๋ฅผ ์ง€์ •ํ•œ ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ‚ค๊ฐ€ ์—†์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • Object put(๊ฐ์ฒด ํ‚ค, ๊ฐ์ฒด ๊ฐ’)์€ ์ง€์ •๋œ ํ‚ค๋ฅผ ์ง€์ •๋œ ๊ฐ’์— ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. ํ‚ค๋„ ๊ฐ’๋„ null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • Object remove(๊ฐ์ฒด ํ‚ค)๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์—์„œ ํ•ญ๋ชฉ(ํ‚ค ๋ฐ ํ•ด๋‹น ๊ฐ’)์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
๋‹ค๋ฅธ ์ค‘์š”ํ•œ ์ž‘์—…:
  • int size()๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ํ•ญ๋ชฉ ์ˆ˜๋Ÿ‰์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • boolean contains(Object value) ์ง€์ •๋œ ๊ฐ’์ด ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฉ”์„œ๋“œ๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • boolean containsValue(Object value)๋Š” ์ง€์ •๋œ ๊ฐ’์ด ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฉ”์„œ๋“œ๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • void clear()๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • boolean containsKey(๊ฐ์ฒด ํ‚ค)๋Š” ์ง€์ •๋œ ํ‚ค๊ฐ€ ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ถ€์šธ isEmpty()๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ•˜๋‚˜ ์ด์ƒ์˜ ํ‚ค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • void rehash()๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๊ณ  ๋ชจ๋“  ํ‚ค๋ฅผ ๋‹ค์‹œ ํ•ด์‹œํ•ฉ๋‹ˆ๋‹ค.

ํ•ด์‹œ ํ…Œ์ด๋ธ” ๊ตฌํ˜„, Java ์ฝ”๋“œ:

ํ•™์ƒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค :

import java.util.Date;
public class Student {
   String surname;
   String name;
   String secondName;
   Long birthday; // Long instead of long is used by Gson/Jackson json parsers and various orm databases

   public Student(String surname, String name, String secondName, Date birthday ){
       this.surname = surname;
       this.name = name;
       this.secondName = secondName;
       this.birthday = birthday == null ? 0 : birthday.getTime();
   }

   @Override
   public int hashCode(){
       //TODO: check for nulls
       return (surname + name + secondName + birthday).hashCode();
   }
   @Override
   public boolean equals(Object other_) {
       Student other = (Student)other_;
       return (surname == null || surname.equals(other.surname) )
               && (name == null || name.equals(other.name))
               && (secondName == null || secondName.equals(other.secondName))
               && (birthday == null || birthday.equals(other.birthday));
   }
}
๋‹ค์Œ์€ Java Hashtable ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. Student ํด๋ž˜์Šค ์˜ ๋‘ ๊ฐœ์ฒด๋ฅผ ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ๋„ฃ์€ ๋‹ค์Œ ์ผ๋ถ€๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ผ๋ถ€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

public class HashTableExample {
   public static void main(String[] args) {
 
       Hashtable<Student, Integer> myHTable = new Hashtable<>();
       Student sarah1 = new Student("Sarah","Connor", "Jane", null);
       Student john = new Student("John","Connor", "Kyle", new Date(1985, 02-1, 28)); // date not exists
       myHTable.put(john,1);
       myHTable.put(sarah1,0);
       System.out.println(myHTable.get(john));
       System.out.println(myHTable.isEmpty());
       System.out.println(myHTable.size());
       System.out.println(myHTable.contains(1));
       myHTable.remove(john);
       System.out.println(myHTable.contains(0));
       System.out.println(myHTable.contains(1));
       System.out.println(myHTable.containsKey(sarah1));
   }
}
ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1
false
2
true
true
false
true

HashMap ๋Œ€ Hashtable

  • Hashtable์€ Java์˜ HashMap๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์€ Hashtable์€ ๋™๊ธฐํ™”๋˜์ง€๋งŒ HashMap์€ ๋™๊ธฐํ™”๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Hashtable์€ ๋™๊ธฐํ™” ๋•Œ๋ฌธ์— HashMap๋ณด๋‹ค ๋Š๋ฆฝ๋‹ˆ๋‹ค.
  • ๋™๊ธฐํ™” ๋ฌธ์ œ๋ฅผ ์ œ์™ธํ•˜๊ณ  Hashtable์€ null์„ ๊ฐ’์ด๋‚˜ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. HashMap์€ ํ•˜๋‚˜์˜ null ํ‚ค์™€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ null ๊ฐ’์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • Hashtable์€ Dictionary ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  HashMap์€ AbstractMap ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค.
  • HashMap์€ Iterator์— ์˜ํ•ด ์ˆœํšŒ๋ฉ๋‹ˆ๋‹ค. Hashtable์€ Iterator๋ฟ ์•„๋‹ˆ๋ผ Enumerator๋กœ๋„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Java ํ•ด์‹œ ํ…Œ์ด๋ธ” ์˜ˆ์ œ(Hashtable ๋Œ€ HashMap null ํ‚ค)

๋‹ค์Œ์€ HashMap ๋ฐ Hashtable ์—์„œ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” null์„ ๋ณด์—ฌ์ฃผ๋Š” ์กฐ๊ฐ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

// Null key Java hashtable example and hashmap example  

try{
      System.out.println("Hashtable");
      Hashtable hashTable = new Hashtable();
      hashTable.put(null, new Object());
    }catch(Exception ex){
      ex.printStackTrace();
    }
    System.out.println("HashMap");
    HashMap hashMap = new HashMap();
    hashMap.put(null, new Object());
    System.out.println("as you see no exceptions with null key in HashMap");
  }
์ด ์กฐ๊ฐ์„ ํฌํ•จํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ:

java.lang.NullPointerException
	at java.base/java.util.Hashtable.put(Hashtable.java:480)
	at Character.main(Character.java:58)
HashMap
as you see no exceptions with null key in HashMap

๊ฒฐ๋ก 

์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ Hashtable์„ ์ž์ฃผ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์ด์ „ ํ”„๋กœ์ ํŠธ์—์„œ ์ด ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด์จŒ๋“  ์ ์–ด๋„ ์ธํ„ฐ๋ทฐ๋ฅผ ์œ„ํ•ด์„œ๋Š” Java์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ์ž‘๋™ ๋ฐฉ์‹์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์œ ์‚ฌ์„ฑ ๋•Œ๋ฌธ์— Hashtable ๋Œ€์‹  HashMap ๊ฐœ์ฒด๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. HashMap์ด ๋” ํšจ๊ณผ์ ์ด๋ฉฐ(๋™๊ธฐํ™”๋˜์ง€ ์•Š์Œ) null์„ ํ‚ค๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION