JAVA/Data Structure

[JAVA] HashMap

๋น…์ฝœํŒ 2022. 10. 8. 01:27
728x90
๋ฐ˜์‘ํ˜•

๐ŸŒŸ ์ฃผ์š” ๋ฉ”์†Œ๋“œ

void clear() ๋ชจ๋“  ๋งคํ•‘์„ ๋งต์œผ๋กœ๋ถ€ํ„ฐ ์‚ญ์ œํ•œ๋‹ค.
Object clone() ํ˜„์žฌ HashMap์„ ๋ณต์ œํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean containsKey(Object key) HashMap์— ์ง€์ •๋œ ํ‚ค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค. (ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด true ๋ฐ˜ํ™˜)
boolean containsValue(Object Value) HashMap์— ์ง€์ •๋œ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค. (ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด true ๋ฐ˜ํ™˜)
Set entrySet() HashMap์— ์ง€์ •๋œ ํ‚ค์™€ ๊ฐ’์„ ์—”ํŠธ๋ฆฌ(ํ‚ค์™€ ๊ฐ’์˜ ๊ฒฐํ•ฉ)์˜ ํ˜•ํƒœ๋กœ Set์— ์ €์žฅํ•˜์—ฌ ๋ฐ˜ํ™˜
V get(Object key) ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ํ‚ค์˜ ๊ฐ’์ธ ๊ฐ์ฒด์™€ ๋งคํ•‘๋˜๊ณ  ์žˆ๋Š” Value๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Object getOrDefalt(Object key, Object defalutValue) ํ‚ค์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ ํ•œ๋‹ค. ํ‚ค๋ฅผ ๋ชป์ฐพ์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’(defalutValue)๋กœ ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean isEmpty() ํ˜„์žฌ ๋ฐ‰์ด ๋น„์–ด์žˆ๋‹ค๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
Set<K> keySet() ๋งต์— ์ €์žฅ๋˜๊ณ  ์žˆ๋Š” key๋“ค์„ set์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
V put(K key, V value) ์ธ์ž๋กœ ์ „๋‹ฌ๋œ key์™€ value๋ฅผ ํ˜„์žฌ ๋งต์— ์ €์žฅํ•œ๋‹ค.
void putAll(Map m) Map์— ์ €์žฅ๋œ ๋ชจ๋“  ์š”์†Œ๋ฅผ HashMap์— ์ €์žฅํ•œ๋‹ค.
V remove(Object key) ์ธ์ž๋กœ ์ „๋‹ฌ๋œ key์— ๋Œ€ํ•œ ๊ฒƒ์ด ์žˆ๋‹ค๋ฉด ํ˜„์žฌ ๋งต์—์„œ ์‚ญ์ œํ•˜๊ณ  ๋งคํ•‘๋œ value๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(key ๋ถ€์žฌ ์‹œ null ๋ฐ˜ํ™˜)
boolean remove(Object key, Object value) ํ•ด๋‹น ๋งต์—์„œ ํŠน์ • ๊ฐ’์— ๋Œ€์‘ํ•˜๋Š” ํŠน์ • ํ‚ค์˜ ๋งคํ•‘์„ ์ œ๊ฑฐํ•œ๋‹ค.
Object replace(Object key, Object value) ์ง€์ •๋œ ํ‚ค์˜ ๊ฐ’์„ ์ง€์ •๋œ ๊ฐ์ฒด(value)๋กœ ๋Œ€์ฒดํ•œ๋‹ค.
boolean replace(Object key,
Object oldValue, Object newValue)
์ง€์ •๋œ ํ‚ค์™€ ๊ฐ์ฒด(oldValue)๊ฐ€ ๋ชจ๋‘ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ƒˆ๋กœ์šด ๊ฐ์ฒด(newValue)๋กœ ๋Œ€์ฒดํ•œ๋‹ค.
int size() ๋งต์— ์ €์žฅ๋œ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Collection<V> values() ํ˜„์žฌ ๋งต์— ์ €์žฅ๋œ value๋“ค๋งŒ Collection ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

1๏ธโƒฃ HashMap ์ˆœํšŒํ•˜๊ธฐ (entrySet, keySet)

  • Map ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” entrySet(), keySet() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

entrySet() ๋ฉ”์„œ๋“œ๋Š” key์™€ value์˜ ๊ฐ’์ด ๋ชจ๋‘ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๊ณ ,
keySet() ๋ฉ”์„œ๋“œ๋Š” key์˜ ๊ฐ’๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • keySet์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐํšŒ ์‹œ Map์—์„œ Key์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์กฐํšŒํ•˜๋Š” ๋น„์šฉ(map.get(key))์ด ์ถ”๊ฐ€๋กœ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ,
    ์„ฑ๋Šฅ์„ ๊ณ ๋ คํ•˜์—ฌ entrySet ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

 

- ์˜ˆ์ œ -

Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "์น˜ํ‚จ");
map.put("b", "ํ”ผ์ž");
map.put("c", "ํŒ…์ˆ˜์œก");
 
// entrySet()
for(Map.Entry<String, Object> entry : map.entrySet()){
    System.out.println("key : " + entry.getKey() + " " + "value : " + entry.getValue());
}

// keySet()
for(String mapKey : map.keySet()){
    System.out.println("key : " + mapKey + " " + "value : " + map.get(mapKey));
}


// iterator ์‚ฌ์šฉ

// entrySet().iterator()
Iterator<Map.Entry<String, Object>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()) {
  Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIterator.next();
  String key = entry.getKey();
  String value = entry.getValue();
  System.out.println("key:" + key + " " + " value:" + value);
}
    
// keySet().iterator()
Iterator<String> keyIterator = map.keySet().iterator();
while (keyIterator.hasNext()) {
    String key = keyIterator.next();
    String value = map.get(key);
    System.out.println("key :" + key + " " + " value:" + value);
}
728x90
๋ฐ˜์‘ํ˜•

'JAVA > Data Structure' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JAVA] ConcurrentHashMap  (0) 2022.10.23
[JAVA] ๋ฐฐ์—ด(Array)๊ณผ ArrayList  (1) 2022.10.13
[JAVA] Queue ์ž๋ฃŒ๊ตฌ์กฐ add์™€ offer ์ฐจ์ด  (0) 2022.10.10