공부

HashMap

빈v 2022. 7. 28. 11:24
728x90

해시맵 선언

HashMap<String, String> map = new HashMap<>();
 
 
 
해시맵 추가
 
map.put("Key","Value");
ex)
map.put("축구","안정환");
map.put("야구","류현진");
 
같은 key 값에 다른 value를 집어넣으면 덮어 씌어진다.
 

 

값 가져오기

map.get("key");
ex)
map.get("축구");

 

key가 있는지 확인

map.containsKey("Key");
 
ex)
map.containsKey("축구");
map.containsKey("농구");
 
true, false로 반환
 
 
 
key값 삭제
 
map.remove("Key");
 
ex)
map.remove("축구");

 

 

value에 List를 이용한 HashMap

HashMap<String,List<String>> topicList = new HashMap<>();
List<String> lst = new ArrayList<>();
topicList.put("a",lst);
       
topicList.get("a").add("cs1");
topicList.get("a").add("cs2");
 
for (String str: topicList.get("a")){
      System.out.println(str);
}

 

 

 

728x90
반응형