HashMap은 Map의 일종으로 key와 value의 쌍으로 이루어진 데이터를 보관합니다.
HashMap은 다음과 같은 특징이 있습니다.
- null key와 null value를 모두 허용합니다.
- 내부적으로 데이터에 접근할 때 동기화를 보장하지 않습니다.
- 데이터의 순서를 보장하지 않습니다.
- 중복된 key값을 허용하진 않지만, 중복된 값은 갖을 수 있습니다.
HashMap은 다음과 같은 API들을 제공합니다. 예제를 통해 어떻게 사용하는지 알아보겠습니다.
- put()
- putAll()
- get()
- remove()
- clear()
- isEmpty()
- keySet()
- values()
- containsKey()
- containsValue()
- replace()
put()
put()
은 인자로 key와 value를 받습니다. 전달된 인자는 HashMap에 key-value 관계로 저장이 됩니다.
public V put(K key, V value)
아래 코드의 HashMap은 key를 String으로, value를 Integer로 사용합니다. put()
으로 데이터를 저장하였습니다.
null은 key, value 모두 허용되며 중복된 key는 마지막에 저장된 값으로 업데이트됩니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
fruits.put(null, 4);
fruits.put("kiwi", 5);
System.out.println("fruits: " + fruits);
// fruits: {banana=2, null=4, apple=1, kiwi=5}
코드의 실행 결과는 주석으로 표기하였습니다.
HashMap.putAll()
putAll()
은 인자로 전달된 Map에 대한 데이터를 모두 저장합니다.
public void putAll(Map<? extends K, ? extends V> m)
다음은 putAll로 두개의 Map을 합치는 예제입니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
Map<String, Integer> food = new HashMap<>();
food.put("coffee", 1);
food.put("hamburger", 2);
food.put("sandwich", 3);
food.putAll(fruits);
System.out.println("food: " + food);
// food: {banana=2, apple=1, kiwi=3, coffee=1, sandwich=3, hamburger=2}
get()
get()
은 인자로 전달된 key에 해당하는 value를 리턴해 줍니다. key가 존재하지 않으면 null
을 리턴합니다.
public V get(Object key) {
다음은 get()으로 value를 가져오는 예제입니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("get(apple): " + fruits.get("apple"));
System.out.println("get(kiwi): " + fruits.get("kiwi"));
System.out.println("get(undefined): " + fruits.get("undefined"));
// get(apple): 1
// get(kiwi): 3
// get(undefined): null
remove()
remove()
는 인자로 전달된 key에 해당하는 데이터를 삭제합니다. 삭제가 되면 value가 리턴됩니다. 존재하지 않는 데이터라면 null
이 리턴됩니다.
public V remove(Object key)
다음 예제를 보시면, 몇몇 데이터를 삭제하고 리턴 값을 확인합니다. 존재하지 않는 데이터를 삭제하려고 하면 null이 리턴됩니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("remove(apple): " + fruits.remove("apple"));
System.out.println("remove(kiwi): " + fruits.remove("kiwi"));
System.out.println("remove(undefined): " + fruits.remove("undefined"));
System.out.println("fruits: " + fruits);
// remove(apple): 1
// remove(kiwi): 3
// remove(undefined): null
// fruits: {banana=2}
clear(), isEmpty()
clear()
는 HashMap의 모든 데이터를 삭제합니다.
public void clear()
isEmpty()
는 HashMap의 데이터가 비어있다면 true
를 리턴하고 아니라면 false
를 리턴합니다.
public boolean isEmpty()
다음 예제를 보시면 HashMap에 데이터가 있을 때 isEmpty() 결과와 clear()로 모든 데이터를 삭제한 후 isEmpty() 결과가 다른 것을 알 수 있습니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("fruits: " + fruits);
System.out.println("is empty? " + fruits.isEmpty());
fruits.clear();
System.out.println("fruits: " + fruits);
System.out.println("is empty? " + fruits.isEmpty());
// fruits: {banana=2, apple=1, kiwi=3}
// is empty? false
// fruits: {}
// is empty? true
keySet(), values()
keySet()
은 HashMap에 저장된 key들을 Set 객체로 리턴해줍니다.
public Set<K> keySet()
values()
는 HashMap에 저장된 value들을 Collection 객체로 리턴해줍니다.
public Collection<V> values()
다음은 key와 value들을 각각 출력하는 예제입니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("keySet(): " + fruits.keySet());
System.out.println("values(): " + fruits.values());
Set<String> keys = fruits.keySet();
for (String key : keys) {
System.out.println("key: " + key);
}
Collection<Integer> values = fruits.values();
for (Integer value : values) {
System.out.println("value: " + value);
}
// keySet(): [banana, apple, kiwi]
// values(): [2, 1, 3]
// key: banana
// key: apple
// key: kiwi
// value: 2
// value: 1
// value: 3
containsKey(), containsValue()
containsKey()
는 HashMap에 인자로 전달된 key가 존재하면 true
를 리턴하고 그렇지 않으면 false
를 리턴합니다.
public boolean containsKey(Object key)
containsValue()
는 HashMap에 인자로 전달된 key가 존재하면 true
를 리턴하고 그렇지 않으면 false
를 리턴합니다.
public boolean containsValue(Object value)
다음 예제는 존재하는 경우와 존재하지 않는 경우에 대한 key, value를 확인해보고 있습니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("containsKey(apple): " + fruits.containsKey("apple"));
System.out.println("containsKey(undefined): " + fruits.containsKey("undefined"));
System.out.println("containsValue(1): " + fruits.containsValue(1));
System.out.println("containsValue(0): " + fruits.containsValue(0));
// containsKey(apple): true
// containsKey(undefined): false
// containsValue(1): true
// containsValue(0): false
replace()
replace()
는 인자로 전달된 key의 value를 인자로 전달된 value로 교체해 줍니다. 교체되어 삭제되는 value는 리턴됩니다.
존재하지 않는 key가 인자로 전달되면 null
이 리턴됩니다.
public V replace(K key, V value)
다음은 존재하는 key와 존재하지 않는 key에 대해서 replace를 시도하는 코드입니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("fruits: " + fruits);
System.out.println("replace(apple, 10): " + fruits.replace("apple", 10));
System.out.println("replace(undefined, 10): " + fruits.replace("undefined", 10));
System.out.println("fruits: " + fruits);
// fruits: {banana=2, apple=1, kiwi=3}
// replace(apple, 10): 1
// replace(undefined, 10): null
// fruits: {banana=2, apple=10, kiwi=3}
인자와 리턴 타입이 다른 replace()
메소드도 있습니다. 인자를 보시면 3개가 있습니다. 첫번째는 key, 두번째는 oldValue, 세번째는 newValue입니다.
저장된 key의 value가 oldValue와 동일할 때만 newValue로 변경해 줍니다. 교체가 되면 true
를 리턴하며, oldValue와 동일하지 않으면 교체되지 않고 false
가 리턴됩니다.
public boolean replace(K key, V oldValue, V newValue)
다음은 oldValue가 일치하여 교체가 되는 경우와, oldValue가 달라 교체되지 않는 경우에 대한 예제입니다.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("fruits: " + fruits);
System.out.println("replace(apple, 1, 10): " + fruits.replace("apple", 1, 10));
System.out.println("replace(banana, 1, 10): " + fruits.replace("banana", 1, 20));
System.out.println("fruits: " + fruits);
// fruits: {banana=2, apple=1, kiwi=3}
// replace(apple, 1, 10): true
// replace(banana, 1, 10): false
// fruits: {banana=2, apple=10, kiwi=3}