1. HashMap 특징
(1) HashSet과 똑같이 순서 보장 x, 중복 허용 x 이다.
(Key값을 기준으로 중복 여부 check 하고, 중복된 Key값이 들어올 시, 가장 최근에 업데이트 된 Value값을 저장한다. )
(2) HashMap은 (Key, Value) 한 쌍으로 이루어져있다.
Key가 사물함 열쇠, Value가 사물함에 들어있는 값이라 생각하면 되겠다.
Key 하나당 Value는 무조건 한 개이다.
(3) Key와 Value는 자료형이 서로 다를 수 있다.
(자료형은 Wrapper 클래스만 가능)
2. 코드 리뷰
import java.util.HashMap;
public class _07_HashMap {
public static void main(String[] args) {
//선언 HashMap<K,V> 이름 = new HashMap<>();
HashMap<String,Integer> map = new HashMap<>();
// 데이터 추가 map.put(K,V)
map.put("유재석", 10);
map.put("박명수", 5);
map.put("김종국", 3);
map.put("서장훈", 15);
System.out.println("총 고객 수: " + map.size());
System.out.println("----------------------------------");
// 조회 map.get(K) = V
System.out.println("유재석님의 포인트 : " + map.get("유재석"));
System.out.println("박명수님의 포인트 : " + map.get("박명수"));
System.out.println("------------------------------------");
// Key값이 있는지 확인
if(map.containsKey("서장훈")) {
int point = map.get("서장훈");
map.put("서장훈", ++point);
System.out.println("서장훈님의 누적 포인트: " + map.get("서장훈"));
} else{
map.put("서장훈",1);
System.out.println("서장훈님 신규 등록 (포인트 1)");
}
System.out.println("--------------------------");
// 삭제
map.remove("유재석");
System.out.println(map.get("유재석"));
System.out.println("-------------------------");
//전체 삭제
map.clear();
if (map.isEmpty()){
System.out.println("남은 고객 수: " + map.size());
System.out.println("가게 접자");
}
System.out.println("---------------------------");
//다시 고객들이 막 찾아옴
map.put("유재석", 10);
map.put("박명수", 5);
map.put("김종국", 3);
map.put("서장훈", 15);
//key 확인
for (String key :
map.keySet()) {
System.out.println(key);
}
System.out.println("------------------------------");
//Value 확인
for (int value :
map.values()) {
System.out.println(value);
}
System.out.println("------------------------------------");
//Key / Value 함께 확인
for (String key :
map.keySet()) {
System.out.println("고객 이름: "+key + "\t포인트: " +map.get(key));
}
System.out.println("------------------------------");
// 맵: 중복 x (맨 마지막에 추가한 값만 들어간다.), 순서 x
map.put("김종국", 10);
map.put("김종국", 30);
map.put("김종국", 50);
for (String key :
map.keySet()) {
System.out.println("고객 이름: "+key + "\t포인트: " +map.get(key));
}
System.out.println("------------------------------");
}
}
3. 스스로 해보기
import java.util.HashMap;
public class HashMap_Myself {
public static void main(String[] args) {
// 선언
HashMap<String, Integer> Goal = new HashMap<>();
// 데이터 추가
Goal.put("Harry Kane", 25);
Goal.put("Halland", 28);
Goal.put("Son_Heung_Min", 21);
Goal.put("Mohamad_Salla", 17);
Goal.put("Hwang_Hee_Chan", 8);
// 조회
System.out.println("해리케인의 득점 수: " + Goal.get("Harry Kane"));
System.out.println("손흥민의 득점 수: " + Goal.get("Son_Heung_Min"));
System.out.println("--------------------------------------------");
// 키 값이 있는지 확인
if(Goal.containsKey("Ronaldo")){
int point = Goal.get("Ronaldo");
Goal.put("Ronaldo", ++point);
} else {
Goal.put("Ronaldo",1);
}
System.out.println("호날두의 득점 수:" + Goal.get("Ronaldo"));
System.out.println("--------------------------------------------");
// 삭제
Goal.remove("Ronaldo");
System.out.println(Goal.get("Ronaldo"));
System.out.println("--------------------------------------------");
// 전체 삭제
Goal.clear();
if(Goal.isEmpty()){
System.out.println("새 시즌을 위해 초기화 완료");
}
System.out.println("--------------------------------------------");
// 다시 값 넣고
Goal.put("Harry Kane", 30);
Goal.put("Halland", 26);
Goal.put("Son_Heung_Min", 24);
Goal.put("Mohamad_Salla", 20);
Goal.put("Hwang_Hee_Chan", 10);
// Key 순회
for (String s :
Goal.keySet()) {
System.out.println("득점 순위권에 든 사람들: "+s);
}
System.out.println("--------------------------------------------");
// Value 순회
for (int i :
Goal.values()) {
System.out.println("득점왕 경쟁자들의 골 수: "+i);
}
System.out.println("--------------------------------------------");
// Key, Value 같이 순회
for (String s :
Goal.keySet()) {
System.out.println("득점 순위권에 든 사람들: " + s + "\t / 골 수: " + Goal.get(s));
}
}
}
'Language > Java' 카테고리의 다른 글
[JAVA] 익명 클래스 (0) | 2023.02.15 |
---|---|
[JAVA] Iterator (0) | 2023.02.15 |
[JAVA]HashSet (0) | 2023.02.09 |
[JAVA] Linked List (0) | 2023.02.08 |
[JAVA] ArrayList (0) | 2023.02.07 |