| Category | HashSet | HashMap |
|---|
| Definition | A collection class that stores unique elements. | A collection class that stores key-value pairs. |
| Data Storage | Stores values (elements) only. | Stores key-value pairs. |
| Duplicates | Does not allow duplicate elements. | Keys cannot be duplicated, but values can be duplicated. |
| Null Values | Allows only one null value. | Allows one null key and multiple null values. |
| Class Hierarchy | Collection โ Set โ HashSet | Map โ HashMap |
| Synchronization | Not synchronized (not thread-safe). | Not synchronized (not thread-safe). |
| Performance | Fast on average (O(1)) for basic operations. | Fast on average (O(1)) for basic operations. |
| Internal Implementation | Internally uses a HashMap (stores values as keys with a dummy object as value). | Uses its own hash table structure internally. |
| Key Methods | add(), remove(), contains(), etc. | put(), get(), remove(), containsKey(), etc. |
| Primary Use | Used to store a collection of unique elements. | Used for efficient lookup/storage of values by key. |
| Examples | Member IDs, unique tag lists, etc. | Mapping structures like product ID โ price, user ID โ age. |
Korean VER.
| ๊ธฐ์ค | HashSet | HashMap |
|---|
| ์ ์ | ๊ณ ์ ํ ์์๋ฅผ ์ ์ฅํ๋ ์ปฌ๋ ์
ํด๋์ค | ํค-๊ฐ ์์ ์ ์ฅํ๋ ์ปฌ๋ ์
ํด๋์ค |
| ๋ฐ์ดํฐ ์ ์ฅ ๋ฐฉ์ | ๊ฐ(value)๋ง ์ ์ฅ | ํค(key)์ ๊ฐ(value)์ ์์ผ๋ก ์ ์ฅ |
| ์ค๋ณต ํ์ฉ | ์ค๋ณต ์์๋ฅผ ํ์ฉํ์ง ์์ | ํค๋ ์ค๋ณต ํ์ฉ๋์ง ์์ง๋ง, ๊ฐ์ ์ค๋ณต ๊ฐ๋ฅ |
| Null ํ์ฉ | ๋จ ํ๋์ null ๊ฐ ํ์ฉ | ํ๋์ null ํค์ ์ฌ๋ฌ ๊ฐ์ null ๊ฐ ํ์ฉ |
| ํด๋์ค ๊ณ์ธต | Collection โ Set โ HashSet | Map โ HashMap |
| ๋๊ธฐํ | ๋น๋๊ธฐํ๋จ | ๋น๋๊ธฐํ๋จ |
| ์ฑ๋ฅ | ํ๊ท ์ ์ผ๋ก ๋น ๋ฆ (O(1)) | ํ๊ท ์ ์ผ๋ก ๋น ๋ฆ (O(1)) |
| ๋ด๋ถ ๊ตฌํ | ๋ด๋ถ์ ์ผ๋ก HashMap์ ์ฌ์ฉ (๊ฐ์ ๋๋ฏธ ๊ฐ์ฒด๋ก ์ ์ฅ) | ์์ฒด์ ์ผ๋ก ํด์ ํ
์ด๋ธ ๊ตฌ์กฐ ์ฌ์ฉ |
| ์ฃผ์ ๋ฉ์๋ | add(), remove(), contains() ๋ฑ | put(), get(), remove(), containsKey() ๋ฑ |
| ์ฌ์ฉ ๋ชฉ์ | ๊ณ ์ ํ ์์๋ค์ ์งํฉ์ ์ ์ฅํ ๋ ์ฌ์ฉ | ํค๋ก ๊ฐ์ ํจ์จ์ ์ผ๋ก ์กฐํ/์ ์ฅํ ๋ ์ฌ์ฉ |
| ์์ | ํ์ ID, ์ ์ผํ ํ๊ทธ ๋ชฉ๋ก ๋ฑ | ์ํID โ ๊ฐ๊ฒฉ, ์ฌ์ฉ์ID โ ๋์ด ๋ฑ ๋งคํ ๊ตฌ์กฐ |