HashMap은 key와 value의 쌍으로 이루어진 데이터를 저장하고 다음과 같은 특징을 가지고 있다.
- key와 value 모두 null 허용
- 데이터 순서를 보장하지 않음
- 중복된 key값 허용하지 않고, 중복된 value값은 갖을 수 있다.
HashMap이 제공하는 API
put()
put()은 key와 value를 쌍으로 받기 때문에 데이터타입을 적어 생성한다.
null은 key와 value 모두 허용되며 중복된 key는 가장 마지막에 저장된 값으로 업데이트된다.
public V put(K key, V value)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put(null, 3);
colors.put("pink", 4);
colors.put("pink", 5);
System.out.println("colors : " + colors);
// colors : {red=1, pink=5, null=3, orange=2}
putAll()
Map과 Map을 합치는 함수
public void putAll(Map<? extends K, ? extends V> m)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
HashMap<String, Integer> achromaticColors = new HashMap<>();
achromaticColors.put("white", 1);
achromaticColors.put("black", 2);
achromaticColors.put("gray", 3);
colors.putAll(achromaticColors);
System.out.println("colors: " + colors);
// colors: {red=1, pink=3, orange=2, gray=3, white=1, black=2}
get()
넘겨지는 파라미터와 같은 key에 해당하는 value를 리턴해준다. key가 존재하지 않으면 null 리턴.
public V get(Object key)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("color: " + colors.get("red"));
// color: 1
remove()
넘겨지는 파라미터와 같은 key에 해당하는 데이터를 삭제합니다.
삭제되면 value 값이 리턴되고, 존재하지 않는 key이면 null이 리턴됩니다.
public V remove(Object key)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("red: " + colors.remove("red"));
System.out.println("orange: " + colors.remove("orange"));
System.out.println("apple: " + colors.remove("apple"));
System.out.println("colors: " + colors);
// red: 1
// orange: 2
// apple: null
// colors: {pink=3}
clear(), isEmpty()
clear()는 HashMap의 모든 데이터를 삭제한다.
public void clear()
isEmpty()는 HashMap이 비어있다면 true, 아니라면 false를 리턴한다.
public boolean isEmpty()
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("color: " + colors);
System.out.println("Is Empty? " + colors.isEmpty());
colors.clear();
System.out.println("color: " + colors);
System.out.println("Is Empty? " + colors.isEmpty());
// color: {orange:2, red:1, pink:3}
// Is Empty? false
// color: {}
// Is Empty? true
keySet(), values()
keySet()은 HashMap에 저장된 key들을 Set 객체로 리턴해준다.
public Set<K> keySet()
values()는 hashMap에 저장된 value 값을 Collection 객체로 리턴해준다.
public Collection<V> values()
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("keySet(): " + colors.keySet());
System.out.println("values(): " + colors.values());
Set<String> keys = colors.keySet();
for(String key : keys) {
System.out.println("key: " + key);
}
Collection<Integer> values = colors.values();
for(Integer val : values) {
System.out.println("value: " + val);
}
//keySet(): [orange, red, pink]
//values(): [3, 1, 2]
//key: orange
//key: red
//key: pink
//value: 3
//value: 1
//value: 2
containsKey(), containValue()
containsKey()는 HashMap에 전달된 파라미터가 key 값에 존재하면 true를 리턴하고 존재하지 않으면 false를 리턴
public boolean containsKey(Object key)
containsValue()는 HashMap에 전달된 파라미터가 value 값에 존재하면 true를 리턴하고 존재하지 않으면 false를 리턴
public boolean containsValue(Object value)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("containsKey(red): " + colors.containsKey("red"));
System.out.println("containsKey(apple): " + colors.containsKey("apple"));
System.out.println("containsValue(1): " + colors.containsValue(1));
System.out.println("containsValue(100): " + colors.containsValue(100));
// containsKey(red): true
// containsKey(apple): false
// containsValue(1): true
// containsValue(100): false
replace()
replace()는 해당 key의 value를 교체해주고 삭제되는 value 값을 리턴
존재하지 않은 key를 변경하려고 하면 null을 리턴
public V replace(K key, V value)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("color: " + colors);
System.out.println("replace(red, 10): " + colors.replace("red", 10));
System.out.println("replace(apple, 10): " + colors.replace("apple", 10));
System.out.println("color: " + colors);
// color: {orange:2, red:1, pink:3}
// replace(red, 10): 1
// replace(apple, 10): null
// color: {orange:2, red:10, pink:3}
replace()에서 파라미터 3개인 경우 HashMap에 저장된 key 값과 value가 oldValue와 동일할때만 newValue로 변경해준다.
key 값과 value 값이 일치해 변경될 경우에는 true를 리턴하고 그렇지 않으면 fasle를 리턴
public boolean replace(K key, V oldValue, V newValue)
HashMap<String, Integer> colors = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("color: " + colors);
System.out.println("replace(red, 10): " + colors.replace("red", 1, 10));
System.out.println("replace(orange, 10): " + colors.replace("orange", 1, 10));
System.out.println("color: " + colors);
// color: {orange:2, red:1, pink:3}
// replace(red, 10): true
// replace(orange, 10): false
// color: {orange:2, red:10, pink:3}
getOrDefault()
getOrDefault()는 전달되는 파라미터와 같은 key 값이 존재하면 해당하는 key 값의 value 값이 리턴되고 그렇지 않으면 DefaultValue 값이 리턴된다. 이 경우 NullPointerException 이 발생되지 않는다.
getOrDefault(Object key, V DefaultValue)
Map<String, Integer> map = new HashMap<>();
colors.put("red", 1);
colors.put("orange", 2);
colors.put("pink", 3);
System.out.println("color: " + colors);
map.put("orange", map.getOrDefault("orange", 0) + 10);
map.put("black", map.getOrDefault("black", 0) + 10);
System.out.println("color: " + colors);
// color: {orange: 2, red: 1, pink: 3}
// color: {orange: 12, red: 10, pink: 3, black: 10}
내가 보려고 정리해두는 HashMap 사용법
'Java' 카테고리의 다른 글
[Java] Arrays 클래스 사용법 (0) | 2022.09.22 |
---|---|
[Java] Int와 Integer의 차이 (1) | 2022.09.21 |
[Java] StringBuffer와 StringBuilder 개념과 사용법 (0) | 2022.09.20 |