Check if Particular Key Exists in Java HashMap
HashMap in Java is the realization of the Hash Table data structure of sorts. It is composed of Key and Value pairs which are symbolically represented as <K,V> where K stands for Key and V for Value. It is an implementation of the Maps Interface and is advantageous in the ways that it provides constant-time performance in assigning and accessing elements via the put and get methods respectively. It is used to form associated pairs and access or modify one based on the other.
There are various approaches to check if particular key exists which are mentioned below :
- Using the built-in containsKey() method of the HashMap class
- Converting the keys of the HashMap to a list and then iterating through them
- Creating a Map from all the entries of the HashMap and then iterating over it
Approach 1 :
Using this approach, we make use of the containsKey() predefined method of the HashMap class which returns a boolean value.
Parameters: The method takes just one parameter key_element that refers to the key whose mapping is supposed to be checked inside a map.
Return Value: The method returns boolean true if the presence of the key is detected else false
- Create a function with a return type of boolean.
- Inside the function, create a new HashMap specifying the data types of the key and the value respectively.
- Fill up the HashMap with Key-Value Pairs using the put() method of the HashMap class.
- Declare a boolean variable to store the result.
- Call the containsKey() method of the HashMap class with the key to be checked as the parameter.
- Return the variable.
Approach 2 :
We create an ArrayList from the keys of the HashMap and then iterate over them to check if the specified key is present or not.
- Repeat steps 1 through 3 as mentioned in the first approach.
- Next, we initialize an ArrayList of the same data type as the keys of the HashMap using the keySet() predefined method of the HashMap class.
- For the next step, we declare an iterator to iterate through the elements of the ArrayList created above.
- We then iterate through the ArrayList checking in each iteration if the particular key matches with any of the elements of the ArrayList created above.
- Return the corresponding output.
Approach 3 :
We iterate over the keys checking if the specified one is present or not.
- Repeat the steps 1 through 3 in the first approach to create a HashMap.
- Using a for each loop and the entrySet() predefined method of the HashMap class, create a Map of the same.
- In each iteration of the for loop, get a key from the Map built above using the built-in getKey() method of the Map class.
- Compare it with the specified key.
- Return true if the key matches with any of the elements of the key set or else return false.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.