check if a key is present in a hashmap code example
Example 1: check if map contains key java
if (map.containsKey(key)) {
// Okay, there's a key but the value is null
} else {
// Definitely no such key
}
Example 2: check if map key has value cpp
#include<map>
int main(){
map<int,char> m;
char ch = '!';
if (m.find(ch) != m.end()) {
std::cout << "Key found";
} else {
std::cout << "Key not found";
}
return 0;
}