import java.util.*;
class Solution4(1) {
public String solution(String[] participant, String[] completion) {
String answer = "";
// 두 집단의 비교를 위하여 먼저 오름차순으로 각 배열을 정렬
Arrays.sort(participant);
Arrays.sort(completion);
// 반복문으로 정렬된 값의 비교에서 일치하지 않는 경우를 찾아줌
for (int i=0;i<completion.length;i++){
if(!participant[i].equals(completion[i])){
answer+=participant[i];
return answer;
}
}
// 위의 반복문에서 일치하지 않는 값이 없다면 배열의 마지막 값이 답이 됨
answer+=participant[participant.length -1];
return answer;
}
}
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
HashMap<String,Integer>map=new HashMap<>();
String answer = "";
for (String key : participant) map.put(key, map.getOrDefault(key,0) + 1);
for (String key : completion) map.put(key, map.get(key) - 1);
for (String key : map.keySet()) {
if (map.get(key) != 0)
return answer = key;
}
return answer;
}
}
Map은 키와 값으로 구성된 Entry객체를 저장하는 구조를 가지고 있는 자료구조입니다.
여기서 키와 값은 모두 객체입니다. 값은 중복 저장될 수 있지만 키는 중복 저장될 수 없습니다.
만약 기존에 저장된 키와 동일한 키로 값을 저장하면 기존의 값은 없어지고 새로운 값으로 대치됩니다
많은 양의 데이터를 검색하는 데 있어서 뛰어난 성능을 보입니다.
**HashMap 선언**
HashMap<String,String> map1 = new HashMap<String,String>();//HashMap생성
HashMap<String,String> map2 = new HashMap<>();//new에서 타입 파라미터 생략가능
HashMap<String,String> map3 = new HashMap<>(map1);//map1의 모든 값을 가진 HashMap생성
HashMap<String,String> map4 = new HashMap<>(10);//초기 용량(capacity)지정
HashMap<String,String> map5 = new HashMap<>(10, 0.7f);//초기 capacity,load factor지정
HashMap<String,String> map6 = new HashMap<String,String>(){{//초기값 지정
put("a","b");
}};
**HashMap 값 추가**
HashMap<Integer,String> map = new HashMap<>();//new에서 타입 파라미터 생략가능
map.put(1,"사과"); //값 추가
map.put(2,"바나나");
map.put(3,"포도");
**HashMap 값 삭제**
map.remove(1); //key값 1 사과 제거
map.clear(); //모든 값 제거
= 오직 키값으로만 Map의 요소를 삭제할 수 있습니다.
**HashMap 값 출력**
System.out.println(map); //전체 출력 : {1=사과, 2=바나나, 3=포도}
System.out.println(map.get(1));//key값 1의 value얻기 : 사과
//entrySet() 활용
for (Entry<Integer, String> entry : map.entrySet()) {
System.out.println("[Key]:" + entry.getKey() + " [Value]:" + entry.getValue());
}
//[Key]:1 [Value]:사과
//[Key]:2 [Value]:바나나
//[Key]:3 [Value]:포도
//KeySet() 활용
for(Integer key : map.keySet()){ //저장된 key값 확인
System.out.println("[Key]:" + i + " [Value]:" + map.get(i));
}
//[Key]:1 [Value]:사과
//[Key]:2 [Value]:바나나
//[Key]:3 [Value]:포도
**Iterator 사용**
//entrySet().iterator()
Iterator<Entry<Integer, String>> entries = map.entrySet().iterator();
while(entries.hasNext()){
Map.Entry<Integer, String> entry = entries.next();
System.out.println("[Key]:" + entry.getKey() + " [Value]:" + entry.getValue());
}
//[Key]:1 [Value]:사과
//[Key]:2 [Value]:바나나
//[Key]:3 [Value]:포도
//keySet().iterator()
Iterator<Integer> keys = map.keySet().iterator();
while(keys.hasNext()){
int key = keys.next();
System.out.println("[Key]:" + key + " [Value]:" + map.get(key));
}
//[Key]:1 [Value]:사과
//[Key]:2 [Value]:바나나
//[Key]:3 [Value]:포도
**containsKey(key)**
System.out.println(hashMap.containsKey("A"));
**containsValue(value)**
System.out.println(hashMap.containsValue("BANANA"));
getOrDefault(Object key, V DefaultValue)
getOrDefault(Object key, V DefaultValue)
**map.put() : HashMap에 키와 값을 저장.**
**map.get() : 지정된 Key 의 값을 반환한다.**