์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- ์ด๋ ธํ ์ด์
- http method
- datepicker
- ์ฑ์ฉํ์ ํ
- URN
- reflection
- url
- IOC
- menutab
- ํ๋ IT&E
- OpenAPI
- 2024-08-21
- fontstyle
- udp
- AndroidStudio
- Dialog
- OOP
- Kotlin
- uri
- swagger
- tcp
- ๊ฐ์ฒด์งํฅํ๋ก๊ทธ๋๋ฐ
- FACTORY
- di
- 2024-08-20
- Factory Method Pattern
- ์ฝ๋์
- Android Studio
- Python
- ๊ธฐ์ด100์
dingdong coding
[ JAVA ] HashMap ๋ณธ๋ฌธ
'ํค์ ๋ํ ํด์ ๊ฐ์ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ ์ฅํ๊ณ ์กฐํํ๋ฉฐ, ํค-๊ฐ ์์ ๊ฐ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ํฌ๊ธฐ๊ฐ ์ฆ๊ฐํ๋ associate array'
associate array : Map, Dictionary, Symbol Table ๋ฑ
HashMap์ ์์๋ณด๊ธฐ ์ Map์ ๋ํด ์์๋ณด์
Map ์ด๋?
Map์ Key-Value์ Mapping์ด๋ค. ์ฆ, ๋ชจ๋ Key๊ฐ ์ ํํ ํ๋์ Value์ Mapping๋๊ณ Key๋ฅผ ์ฌ์ฉํ์ฌ Map์์ ํด๋น Value๋ฅผ ๊ฒ์ํ ์ ์์ต๋๋ค.
๋ํ Key์ Value๋ ๋ชจ๋ ๊ฐ์ฒด๋ก Value๋ ์ค๋ณต์ ์ฅ๋ ์ ์์ง๋ง Key๋ ์ค๋ณต์ ์ฅ์ด ๋ถ๊ฐํฉ๋๋ค.
๋จ์ํ List์ ๊ฐ์ ์ถ๊ฐ ํ์ง ์๋ ์ด์ , HashMap์ด ํ์ํ ์ด์ ๋ ๋ฌด์์ผ๊น? ๊ฐ๋จํ ์ด์ ๋ ์ฑ๋ฅ๋๋ฌธ์ ๋๋ค.
List์์ ํน์ ์์๋ฅผ ์ฐพ์ผ๋ ค๋ฉด ์๊ฐ ๋ณต์ก๋๊ฐ O(n), ์ด์งํ์์ ์ฌ์ฉํ์ฌ ๋ชฉ๋ก ์ ๋ ฌ ์ ์๊ฐ ๋ณต์ก๋๊ฐ O(log n) ์ด ๋ฉ๋๋ค.
HashMap ์ ์ฅ์ ์ ๊ฐ์ ์ฝ์ ํ๊ณ ๊ฒ์ํ๋ ์๊ฐ ๋ณต์ก๋๊ฐ ํ๊ท O(1) ์ด๋ผ๋ ๊ฒ์ ๋๋ค.
HashMap ์ด๋?
HashMap์ Map์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์๋ ๋ํ์ ์ธ ํด๋์ค์ ๋๋ค. ์ด๋ฆ ๊ทธ๋๋ก ํด์ฑ(Hashing)์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ง์ ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๋๋ฐ ์์ด์ ๋ฐ์ด๋ ์ฑ๋ฅ์ ๋ณด์ ๋๋ค.
HashMap์ ํด์ํจ์๋ฅผ ํตํด ํค์ ๊ฐ์ด ์ ์ฅ๋๋ ์์น๋ฅผ ๊ฒฐ์ ํ๋ฏ๋ก ์ฌ์ฉ์๋ ๊ทธ ์์น๋ฅผ ์ ์์๊ณ , ์ฝ์ ๋๋ ์์์ ์์น ๋ํ ๊ด๊ณ๊ฐ ์์ต๋๋ค.
1) HashMap์ ๋ชจ๋ ์์๋ฅผ ๋ฐ๋ณตํ๋ ๋์ ํค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ ์์น๋ฅผ ๊ณ์ฐํ๋ค.
2) ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ๋ณ๊ฒฝํ ์ ์๋ ํค๋ฅผ ์ฌ์ฉํด์ผํ๊ฑฐ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ํค ์ฌ์ฉ์ ์ต์ํ ๊ฒฐ๊ณผ๋ฅผ ์๊ณ ์์ด์ผ ํ๋ค.
์์ ๋ฅผ ํตํด HashMap์ ์ฌ์ฉ๋ฒ์ ๋ํด ์์๋ด ์๋ค.
1. Set up
๋จผ์ ๊ฐ๋จํ class๋ฅผ ์์ฑํด๋ณด์
public class Product {
private String name;
private String description;
private List<String> tags;
// standard getters/setters/constructors
public Product addTagsOfOtherProduct(Product product) {
this.tags.addAll(product.getTags());
return this;
}
}
2. Put
์ด์ String ์ ํ์ Key์ Product ์ ํ์ ์์๋ฅผ ์ฌ์ฉํ์ฌ HashMap์ ์์ฑํ ์ ์๋ค.
Map<String, Product> productsByName = new HashMap<>();
๊ทธ๋ฆฌ๊ณ HashMap์ ์ ํ์ ์ถ๊ฐํ์
Product eBike = new Product("E-Bike", "A bike with a battery");
Product roadBike = new Product("Road bike", "A bike for competition");
productsByName.put(eBike.getName(), eBike);
productsByName.put(roadBike.getName(), roadBike);
3. Get
Key๋ก Map์์ ๊ฐ์ ๊ฒ์ํ ์ ์๋ค.
Product nextPurchase = productsByName.get("E-Bike");
assertEquals("A bike with a battery", nextPurchase.getDescription());
๋ง์ฝ Map์ ์กด์ฌํ์ง ์๋ ํค์ ๊ฐ์ ๊ฒ์ํ๋ฉด null๊ฐ์ ์ป๊ฒ ๋๋ค.
Product nextPurchase = productsByName.get("Car");
assertNull(nextPurchase);
์ด๋ฏธ ์กด์ฌํ๋ ๋์ผํ ํค๋ฅผ ์ฝ์ ํ๋ฉด ๋ง์ง๋ง์ ์ฝ์ ๋ ๊ฐ์ ๊ฐ์ ธ์จ๋ค.
Product newEBike = new Product("E-Bike", "A bike with a better battery");
productsByName.put(newEBike.getName(), newEBike);
assertEquals("A bike with a better battery", productsByName.get("E-Bike").getDescription());
4. Null as the Key
HashMap์ ์ฌ์ฉํ๋ฉด null์ ํค๋ก ์ฌ์ฉํ ์์๋ค.
Product defaultProduct = new Product("Chocolate", "At least buy chocolate");
productsByName.put(null, defaultProduct);
Product nextPurchase = productsByName.get(null);
assertEquals("At least buy chocolate", nextPurchase.getDescription());
5. Values with the Same Key
๋ํ ๋ค๋ฅธ ํค๋ก ๋์ผํ ๊ฐ์ฒด๋ฅผ ๋ ๋ฒ ์ฝ์ ํ ์ ์๋ค.
productsByName.put(defaultProduct.getName(), defaultProduct);
assertSame(productsByName.get(null), productsByName.get("Chocolate"));
6. Remove Value
HashMap์์ Key-Value ๋งคํ ์ ๊ฑฐ
productsByName.remove("E-Bike");
assertNull(productsByName.get("E-Bike"));
7. Check If a Key or Value Exists in the Map
Map์ Key๋๋ Value๊ฐ ์๋์ง ํ์ธํ๋ ค๋ฉด containsKey(), containsValue() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
productsByName.containsKey("E-Bike");
productsByName.containsValue(eBike);
ํด๋น ์์ ์์ ๋ ๋ฉ์๋๋ ๋ชจ๋ true๋ฅผ ๋ฐํํ๋ค. ๋งค์ฐ ์ ์ฌํด๋ณด์ด์ง๋ง ์ด ๋ ๋ฉ์๋ ํธ์ถ์๋ ์ฑ๋ฅ์ฐจ์ด๊ฐ ์๋ค.
ํค๊ฐ ์๋์ง ํ์ธํ๋ ๋ณต์ก๋๋ O(1)์ธ ๋ฐ๋ฉด ๊ฐ์ ํ์ธํ๋ ๋ณต์ก๋๋ Map์ ๋ชจ๋ ์์๋ฅผ ๋ฐ๋ณตํด์ผํ๊ธฐ ๋๋ฌธ์ O(n)์ด๋ค.
8. Iterating Over a HashMap
Key
for(String key : productsByName.keySet()) {
Product product = productsByName.get(key);
}
Key-Value
for(Map.Entry<String, Product> entry : productsByName.entrySet()) {
Product product = entry.getValue();
String key = entry.getKey();
//do something with the key and value
}
Value
List<Product> products = new ArrayList<>(productsByName.values());
'๐ฐJAVA > Basic' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA ] List Interface (0) | 2022.06.20 |
---|---|
[JAVA ] Collection (0) | 2022.06.20 |
[ JAVA ] Optional (0) | 2022.06.15 |
[ JAVA ] Lambda (0) | 2022.06.14 |
[ JAVA, DB ์์ฃผ ] ๊ฐ๋ฐ ๊ด๋ จ ์ฉ์ด ์ ๋ฆฌ ( ๊ฐ์ธ ํ์ต์ฉ ) (0) | 2022.06.14 |