HashMap은 키-값 쌍으로 데이터를 저장하는 해시 기반의 맵(Collection) 구조이다. 각 키는 고유해야 하며, 중복된 키를 허용하지 않는다. 값은 중복이 가능하며, 하나의 키에 대해 하나의 값만 연결할 수 있다.
put(K key, V value): 키와 값을 추가하거나, 동일한 키가 존재할 경우 값을 덮어쓴다.get(Object key): 키를 통해 값을 가져온다. 키가 없을 경우 null을 반환한다.remove(Object key): 특정 키를 삭제하고, 삭제된 값을 반환한다.containsKey(Object key): 주어진 키가 존재하는지 확인한다. 키가 존재하면 true, 존재하지 않으면 false를 반환한다.containsValue(Object value): 특정 값이 존재하는지 확인한다. 값이 존재하면 true, 존재하지 않으면 false를 반환한다.//HashMap 생성
HashMap<String, String> map = new HashMap<>();
// 1. put() - 키와 값을 추가
map.put("apple", "red");
map.put("banana", "yellow");
map.put("grape", "purple");
System.out.println(map);
// {apple=red, banana=yellow, grape=purple}
// 2. get() - 특정 키에 해당하는 값 가져오기
String appleColor = map.get("apple");
System.out.println(appleColor); // red
// 3. containsKey() - 특정 키가 존재하는지 확인
boolean hasBanana = map.containsKey("banana");
System.out.println(hasBanana); // true
// 4. containsValue() - 특정 값이 존재하는지 확인
boolean hasGreen = map.containsValue("green");
System.out.println(hasGreen); // false
// 5. remove() - 특정 키와 해당 값을 제거
String removedValue = map.remove("grape");
System.out.println(removedValue); // purple
System.out.println(map); // {apple=red, banana=yellow}
entrySet()은 Map.Entry 객체의 집합을 반환하므로, 키와 값을 동시에 순회할 수 있다.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// entrySet을 이용한 순회
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
keySet()은 키만 반환하므로, 키를 통해 값에 접근할 수 있다.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// keySet을 이용한 순회
for (String key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
}
}
values()는 값만 반환하므로, 값만 순회할 때 유용하다.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// values()를 이용한 순회
for (Integer value : map.values()) {
System.out.println("Value: " + value);
}
}
}
HashSet은 중복을 허용하지 않는 유일한 값들을 저장하는 해시 기반의 집합(Collection) 구조이다. 각 요소는 고유해야 하며, 순서를 보장하지 않는다.
add(E e): 요소를 추가한다. 이미 존재하는 요소는 추가되지 않는다.remove(Object o): 특정 요소를 제거한다. 제거 성공 시 true를 반환하고, 제거 못하면 false를 반환한다. contains(Object o): 특정 요소가 존재하는지 확인한다. 요소가 존재하면 true, 존재하지 않으면 false를 반환한다.isEmpty(): 집합이 비어 있는지 확인한다. 비어있으면 true, 비어있지 않으면 false를 반환한다.// HashSet 생성
HashSet<String> set = new HashSet<>();
// 1. isEmpty() - HashSet이 비어 있는지 확인
System.out.println(set.isEmpty()); // true;
// 2. add() - 요소 추가
set.add("apple");
set.add("banana");
set.add("cherry");
System.out.println(set); // [banana, cherry, apple]
// 3. contains() - 특정 요소가 존재하는지 확인
boolean hasApple = set.contains("apple");
System.out.println(hasApple); // true;
// 4. remove() - 특정 요소 제거
boolean removedCherry = set.remove("cherry");
System.out.println(removedCherry); // true
System.out.println(set); // [banana, apple]
// 5. 다시 isEmpty() 확인
System.out.println(set.isEmpty()); // false
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("One");
set.add("Two");
set.add("Three");
// for-each를 이용한 순회
for (String value : set) {
System.out.println("Value: " + value);
}
}
}
import java.util.HashSet;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("One");
set.add("Two");
set.add("Three");
// Iterator를 이용한 순회
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println("Value: " + iterator.next());
}
}
}