Done is better than perfect
[Java] HashMap 정리 본문
HashMap의 장점: 데이터 추가, 삭제 특히 검색이 빠름!
import java.util.HashMap;
//HashMap 생성방법
HashMap<String, String> example = new HashMap<String, String>( );
//데이터 추가
example.put("key1", "value");
exmaple.putIfAbsent("key2", "value"); //key가 없으면 저장하기
//데이터 수정
example.replace("key1", "cc");
//데이터확인
System.out.println(example.containsKey("key1"));
//사이즈
System.out.println(example.size());
//데이터 반환
System.out.println(example.get("key1"));
//모든 keySet
Set s=example.keySet();
//모든 value
Collection<Integer> c=example.values();
Map.Entry
Map<String, String> example = new HashMap<>();
for (Map.Entry<String, String> entry : example.entrySet()) {
System.out.println("key : " + entry.getKey());
System.out.println("value : " + entry.getValue());
}
Map.Entry 메서드
- getKey(): Map.Entry 의 key값을 반환
- getValue(): Map.Entry 의 value값을 반환
- setValue(value): Map.Entry 객체의 key에 대한 value값을 수정할 수 있다.
'Java' 카테고리의 다른 글
[Java] 가비지 컬렉터 (1) | 2024.10.21 |
---|---|
[Java] Java가 실행되는 과정 (0) | 2024.10.21 |
[Java] 특징 정리 (2) | 2024.10.10 |
[Java] 기본 자료형 wrapper class 차이 (2) | 2024.09.09 |
[JAVA] priority Queue (0) | 2024.03.15 |