Example
Verify the existence of a key within a map.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin"); capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC"); System.out.println(capitalCities.containsKey("England"));
System.out.println(capitalCities.containsKey("Canada"));
}
}
|
Definition and Usage
The containsKey() method returns true if a map contains an entry with the specified key, and false otherwise.
Syntax
public boolean containsKey(Object key)
|
Parameter Values
|
Parameter
|
Description
|
|
key
|
Required: The key to be searched for.
|
Technical Details
|
Returns:
|
It returns true if an entry with the specified key exists in the map; otherwise, it returns false.
|