HashMap이란?
- Map인터페이스의 한종류로써 Key와 Value 값으로 데이터를 저장하는 형태
- Map 종류 : Hashtable, HashMap, LinkedHashMap, SortedMap, TreeMap 등등
- 그 중 Hash Map은 해싱 검색방법을 이용하기 때문에 뛰어난 성능을 보여줌 .
주요 method
- map.put(Key, Value)
- map.remove(key)
- map.size()
- map.get(key)
- map.clear()
- map.clone()
- map.containsKey(key) : Key값을 가진 엘리먼트가 있으면 true
- map.containsValue(value) : Value값을 가진 엘리먼트가 있으면 true
package OneDayOneCodingChallenge;
import java.util.HashMap;
import org.junit.Assert;
import org.junit.Test;
/**
* @author seoyeon on 2020/09/14
* @project AlgorithmTest 프로그래머 위장 HashMap을 이용한 문제
*/
public class Day9 {
public int solution(String[][] clothes) {
int answer = 1;
HashMap<String, Integer> map = new HashMap<>(); // key, value구조로 값을 저장함.
for (String[] cloth : clothes) { // String 배열을 이렇게 한줄 씩 처리할 수 있구나.
map.put(cloth[1], map.getOrDefault(cloth[1], 0) + 1); // map.getOrDefault 찾는 키값이 있으면 그것을 반환하고, 없으면 defaultvalue로 선언한 값을 반환.. 여기선 0이 default값
}
for(String key :map.keySet()){
answer =answer*(map.get(key)+1);
}
return answer-1;
}
@Test
public void 정답() {
Assert.assertEquals(5, solution(new String[][]
{{"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear"},
{"green_turban", "headgear"}}));
}
}