chapter 15 Map~
Map의 관하여
다른 언어에서는 dictionary라고 불리우기도 함
word: desc (단어 설명) 쌍으로 이루어져있음
API에서
HashMap주로 사용 예전 버전에서는 Hashtable도 사용 하기도 했다.
HashMap<K,V> 키에해당하는타입, 벨류의해당하는타입
//put : key : value 쌍 추가
// size : map의 크기
// get : key로 value를 얻기
ex) Integer v1 = map.get(100);
System.out.println(v1); 키값 대입시 value를 리턴함
//없는 키값 넣으면 null이 리턴
// put : 이미 있는 key로 value를 추가 -> 덮어씀
덮어씌웠으면 크기는 안 바뀌겟죠?
// keySet : key 집합(set) 리턴
키는 중복이 안됨
// 전체 key : value 쌍(entry) 탐색
package p15collection.p03lecture.p03map;
public class Ex01
//Map
//key:value 쌍으로 저장
결과값
이어서 보면
결과값
탐색1,탐색2 방법 확인 하기
// 전체 key : value 쌍(entry) 탐색
// 1. key set 얻고
// 2. key로 value 탐색
System.out.println("===탐색1===");
keys = map.keySet(); // 1. key set 얻고
for ( Integer key : keys) { //2. key 로 value 탐색
System.out.println(key + ":" + map.get(key));
}
System.out.println("===탐색2====");
// Entry 메소드 사용 (Map 안에잇는 Entry 메소드 사용)
Set<Map.Entry<Integer, Integer>> entrys = map.entrySet();
(Set의 아이템 타입이 Integer)
for (Map.Entry<Integer, Integer> entry : entrys ) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
for ( 배열의 값을 담을 변수 : 배열의 변수이름 ) {
실행문
}
항상된 for문 적용된거입니다. 보면 복잡해 보이는데 이 개념을 생각하면서 for 문을 읽어 보자
API 에서 Interface Map.Entry<K,V>에서 메소드 확인
이 외에도 많이 존재
https://docs.oracle.com/javase/8/docs/api/
에서 확인
containsKey(Object value)
equals(Object o)
isempty()
remove(Object key)
replace(K key, V value) 등등..
// Make a new empty map Map<String, String> map = new HashMap<String, String>();
map.get(key) -- retrieves the stored value for a key, or null if that key is not present in the map.
map.put(key, value) -- stores a new key/value pair in the map. Overwrites any existing value for that key.
map.containsKey(key) -- returns true if the key is in the map, false otherwise.
map.remove(key) -- removes the key/value pair for this key if present. Does nothing if the key is not present.
package p15collection.p02quiz.p03map;
public class Ex01
Ex01
// 1:2, 2:4 엔트리 쌍을 넣어라
Ex01Test
public class Ex02
//map을 만들어서 return
// 1:2, 2:4 키벨류추가
Ex02Test
public class Ex03
//map을 만들어서 return
// 키:벨류 총 5개
//키 : 1~5
// 밸류 : 키 *3
내 답변
Ex03Test
public class Ex04
// list에 있는 String을 키, 그 문자열(key)의 길이를 value로 같는
// map를 만들어서 return
Ex04Test
EX05
// param이 가지고 있는 value를 2배한 새 맵을 만들어서re turn
// param이 변경되면 안됨
EX05Test
무엇을 사용했니?
Ex05 에서 EntrySet 이용
Ex05Test map3
public class Ex06
// 파라미터 map의 각 값(value)을 2배로
키값에 다시 값을 넣으면 그 값이 적용 되는것을 활용한것,
또는 replace 메소드이용 가능
Map<Integer, Integer> map1 = new HashMap<>(); 를 만들 필요가 없었음
그냥 다시 넣거나 replace로 교체해주면 되니깐
Ex06Test
public class Ex07
//param의 size가 2
//param의 키는 "a" , "b"
//두 value를 서로 바꾼(swap) map을 만들어서 return
//parameter 변경 x
Ex07 선생닙답변
선생님이 키는 a,b를 지정해줘서 그냥 바꿔주면됨
Ex07내답변..(뭔가 어설프게따라함 + 괜히 코드작성더한듯한느낌)
Ex07Test
public class Ex08 {
// param size : 2
// param 키는 "a", "b"
// param 각 값을 변경(swap)
난 이거 보고 사용했음..
내가 한 Ex08
선생님 답변
Ex08Test
public class Ex09
//str의 문자가 몇번 나오는지
//문자를 key 몇번을 value 로 갖는 map 만들어서 return
charAt 사용하는것은 생각 했는디...
Ex09
list, set 에서 사용했던 방식과 유사하기도하네
Ex09Test
public class Ex10
//map1과 map2에 키가 둘다 있으면 베률를 더해서 추가
// 한 쪽에만 있으면 그냥 추가
// 한 새 map을 만들어서 리턴
Ex10
Ex10 map1은 entrySet map2는 Iterator 이용해서 조금 응용한 것
Ex10Test
Set<Map.Entry<String, Integer>> entries = map1.entrySet();
<Map.Entry<String, Integer> Set 쓸껀디 entrySet.에 있는 set콜렉션쓸꺼야..?
Iterator<Map.Entry<String, Integer>> iteratorE = map2.entrySet().iterator();
<Map.Entry<String, Integer> Iterator 쓸껀디 entrySet안에 .iterator에서 쓸꺼야..?
'Java 공부 > 자바 공부 (baek)' 카테고리의 다른 글
2021-09-27 13.4,5,6 제네릭 + 와일드카드 (0) | 2021.09.27 |
---|---|
2021-09-24 (0) | 2021.09.24 |
2021-09-17 Collection Set~ (0) | 2021.09.17 |
2021-09-16 (0) | 2021.09.16 |
2021-09-15 15장 컬렉션 프레임워크01 (0) | 2021.09.15 |