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.
Syntax:
Hash_Map.containsKey(key_element)
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
Algorithm :
- 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.
Code :
Java
import java.util.*;
class GFG {
boolean checkForKey(String keyToBeChecked)
{
HashMap<String, Integer> hashMap =
new HashMap<>();
hashMap.put( "first" , 1 );
hashMap.put( "second" , 2 );
hashMap.put( "third" , 3 );
hashMap.put( "fourth" , 4 );
boolean result
= hashMap.containsKey(keyToBeChecked);
return result;
}
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println(ob.checkForKey( "fourth" ));
}
}
|
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.
Algorithm :
- 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.
Code :
Java
import java.util.*;
class GFG {
boolean checkForKey(String keyToBeChecked)
{
HashMap<String, Integer> hashMap =
new HashMap<>();
hashMap.put( "first" , 1 );
hashMap.put( "second" , 2 );
hashMap.put( "third" , 3 );
hashMap.put( "fourth" , 4 );
ArrayList<String> listOfKeys
= new ArrayList<>(hashMap.keySet());
Iterator<String> itr = listOfKeys.iterator();
while (itr.hasNext()) {
if (itr.next() == keyToBeChecked)
return true ;
}
return false ;
}
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println(ob.checkForKey( "second" ));
}
}
|
Approach 3 :
We iterate over the keys checking if the specified one is present or not.
Algorithm :
- Repeat 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.
Code :
Java
import java.util.*;
class GFG {
boolean checkForKey(String keyToBeChecked)
{
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put( "first" , 1 );
hashMap.put( "second" , 2 );
hashMap.put( "third" , 3 );
hashMap.put( "fourth" , 4 );
for (Map.Entry<String, Integer> mapEntries :
hashMap.entrySet()) {
if (mapEntries.getKey() == keyToBeChecked)
return true ;
}
return false ;
}
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println(ob.checkForKey( "seventh" ));
}
}
|
Last Updated :
20 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...