![]() |
VOOZH | about |
The containsKey() method in Java is used to check if a given key exists in a TreeMap. If the key is present in the map, it returns true, otherwise, it returns false. This method is very helpful when we need to verify whether a mapping exists before performing any operation on it.
treeMap.containsKey(key);
Example 1: In this example, we are going to use a TreeMap with Integer keys and String values. We will check the presence of keys using the containsKey() method.
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Is the key '20' present? true
Is the key '5' present? false
Example 2: In this example, we are going to create a TreeMap with String keys and Integer values. Here, we will check for the existence of some string keys.
Initial Mappings are: {4=15, Geeks=20, Welcomes=25, You=30}
Is the key 'Welcomes' present? true
Is the key 'World' present? false
Important Points: