개발 지식 기록/JAVA

[메서드 정리] HashMap

엉망진창좌충우돌 2023. 8. 18. 00:11

◎ clear

저장된 모든 객체 제거

HashMap<String, Integer> map = new HashMap<>();
map.clear();

※ HashMap을 초기화 할 때 clear() vs new HashMap<>() 중 어느 것을 써야할까?

- 일반적으로 new HashMap<>()을 사용하는 것이 유리

clear()는

내부적으로 해시 버킷 등을 다시 할당하고 원래 상태로 돌리는 작업을 수행하기 때문에 상대적으로 오버헤드가 크다. 따라서 새로운 인스턴스를 생성하는 new HashMap<>()이 일반적으로는 더 빠르다. (상황에 따라 clear()가 빠른 경우가 있지만 일반적으로는 느리다.)

 

◎ clone

현재 HashMap 복사

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
HashMap<String,Integer> map2 = (HashMap<String, Integer>) map.clone();

 

◎ constainsKey / constainsValue

HashMap에 지정된 key(Value)가 포함되어 있는지 알려준다.

 HashMap<String,Integer> map = new HashMap<>();
 map.put("철수",1);
 if(map.containsKey("철수")){
 	System.out.println("철수있음");
 }
 if(map.containsValue(1)){
 	System.out.println("1도 있음");
 }

 

◎ entrySet

HashMap에 저장된 키와 값을 엔트리(키와 값 결합 형태) 형태로 Set에 저장해서 반환

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);
for(Map.Entry<String,Integer>entry:map.entrySet()){
	System.out.println("key : "+entry.getKey()+", value : "+entry.getValue());
}

 

◎ get(key) / getOrDefault(key, default value)

저장된 key의 객체값 반환. getOrDefault는 해당 key가 없으면 defaultValue로 지정된 객체 반환

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);
System.out.println(map.get("철수")); // 1
System.out.println(map.getOrDefault("돼지",5)); //5

 

◎ isEmpty

HashMap이 비어있는지 여부를 알려준다.

HashMap<String,Integer> map = new HashMap<>();
if(map.isEmpty()){
	System.out.println("비어있음");
}

 

◎ keySet

HashMap에 저장된 모든 키가 저장된 Set을 반환

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);
for(String key :map.keySet()){
	System.out.println("key : "+key+", value : "+ map.get(key));
}

 

◎ put

지정된 키와 값을 HashMap에 저장

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);
for(String key :map.keySet()){
	System.out.println("key : "+key+", value : "+ map.get(key));
}

 

◎ putAll(Map m)

map에 저장된 모든 요소를 HashMap에 저장

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);

HashMap<String,Integer> map2 = new HashMap<>();
map2.put("철수",2);
map2.put("영희",1);
map2.put("덕팔",3);
map2.put("용철",4);

map.putAll(map2);
System.out.println(map);
//{철수=2, 영희=1, 덕팔=3, 용철=4}

 

◎ remove(key)

지정된 key로 저장된 값 제거

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);

map.remove("영희");
System.out.println(map);
//{철수=1, 덕팔=3}

 

replace(Object key, value) / replace(Object key, Object oldValue, Object newValue)

지정된 key의 값을 value로 대체. 인자가 3개인 경우 key와 oldValue가 모두 일치해야 newValue로 대체

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);

map.replace("영희",6);
System.out.println(map);//{철수=1, 영희=6, 덕팔=3}
map.replace("영희",6,7);
System.out.println(map);//{철수=1, 영희=7, 덕팔=3}

 

◎ size

저장된 요소의 개수를 반환

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);

System.out.println(map.size());//3

 

◎ value

저장된 모든 값을 컬렉션 형태로 반환

HashMap<String,Integer> map = new HashMap<>();
map.put("철수",1);
map.put("영희",2);
map.put("덕팔",3);

System.out.println(map.values());//[1, 2, 3]