티스토리 뷰

Map에서 Key가 있는지 없는지 확인 [ containsKey ]

public class mapContainsKey {
	public static void main(String[] args) {
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("A","아프리카");
		hashMap.put("B","브라질");
		hashMap.put("C","체코");
		hashMap.put("D","덴마크");
		System.out.println(hashMap.containsKey("A")); // true
		System.out.println(hashMap.containsKey("E")); // false
	}
}

 

Map에서 Value가 있는지 확인 [containsValue]

public class mapContainsValue {
	public static void main(String[] args) {
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("A","아프리카");
		hashMap.put("B","브라질");
		hashMap.put("C","체코");
		hashMap.put("D","덴마크");
		System.out.println(hashMap.containsValue("브라질")); // true
		System.out.println(hashMap.containsValue("영국")); // false
	}
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함