The Wayback Machine - https://web.archive.org/web/20240821084634/https://www.geeksforgeeks.org/hashmap-containskey-method-in-java/
Open In App

HashMap containsKey() Method in Java

Last Updated : 02 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The java.util.HashMap.containsKey() method is used to check whether a particular key is being mapped into the HashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.

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 .
Below programs are used to illustrate the working of java.util.HashMap.containsKey() Method: 
Program 1: Mapping String Values to Integer Keys. 

Java




// Java code to illustrate the containsKey() method
import java.util.*;
 
public class Hash_Map_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty HashMap
        HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
 
        // Mapping string values to int keys
        hash_map.put(10, "Geeks");
        hash_map.put(15, "4");
        hash_map.put(20, "Geeks");
        hash_map.put(25, "Welcomes");
        hash_map.put(30, "You");
 
        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hash_map);
 
        // Checking for the key_element '20'
        System.out.println("Is the key '20' present? " +
        hash_map.containsKey(20));
 
        // Checking for the key_element '5'
        System.out.println("Is the key '5' present? " +
        hash_map.containsKey(5));
    }
}


Output: 

Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Is the key '20' present? true
Is the key '5' present? false

 

Program 2: Mapping Integer Values to String Keys. 

Java




// Java code to illustrate the containsKey() method
import java.util.*;
 
public class Hash_Map_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty HashMap
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>();
 
        // Mapping int values to string keys
        hash_map.put("Geeks", 10);
        hash_map.put("4", 15);
        hash_map.put("Geeks", 20);
        hash_map.put("Welcomes", 25);
        hash_map.put("You", 30);
 
        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hash_map);
 
        // Checking for the key_element 'Welcomes'
        System.out.println("Is the key 'Welcomes' present? " +
        hash_map.containsKey("Welcomes"));
 
        // Checking for the key_element 'World'
        System.out.println("Is the key 'World' present? " +
        hash_map.containsKey("World"));
    }
}


Output: 

Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
Is the key 'Welcomes' present? true
Is the key 'World' present? false

 

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.

Time Complexity:

Time Complexity of HashMap.containsKey() is O(1) in Average Case, and O(n) in worst case.
 



Previous Article
Next Article

Similar Reads

Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size()) In this post more methods are discussed. keySet(): java.util.HashMap.keySet() It returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. Syntax: public Set keySet() Retu
6 min read
Java.util.TreeMap.containskey() and containsValue() in Java
There are two variants of contains() in Java.util.TreeMap, both are discussed in this article. 1. containskey(Object o) : It returns true if the map contains a mapping for the specified key. Parameters: o : The key which will be tested whether present or not. Return Value: Returns true if there is a mapping for the given key. Exception: ClassCastEx
2 min read
LinkedHashMap containsKey() Method in Java with Examples
The java.util.LinkedHashMap.containsKey() method is used to check whether a particular key is being mapped into the LinkedHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.Syntax: Linked_Hash_Map.containsKey(key_element) Parameters: The method takes just one parameter key_element that refe
2 min read
Hashtable containsKey() Method in Java
The java.util.Hashtable.containsKey() method is used to check whether a particular key is present in the Hashtable or not. It takes the key element as a parameter and returns True if that element is present in the table. Syntax: Hash_table.containsKey(key_element) Parameters: The method takes just one parameter key_element which refers to the key w
2 min read
TreeMap containsKey() Method in Java
The java.util.TreeMap.containsKey() method is used to check whether a particular key is being mapped in the TreeMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: Tree_Map.containsKey(key_element) Parameters: The method takes just one parameter key_element that refers to the key whose
2 min read
EnumMap containsKey() Method in Java
The Java.util.EnumMap.containsKey(key) method is used to check whether a specified key mentioned in the parameter is present in this map or not. Syntax: boolean containsKey(Object key) Parameter: The method accepts one parameter key which refer to the key to be verified. Return Value: The method returns true if the key is present in the EnumMap oth
2 min read
WeakHashMap containsKey() Method in Java
The java.util.WeakHashMap.containsKey() method is used to check whether a particular key is being mapped into the WeakHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: Weak_Hash_Map.containsKey(key_element) Parameters: The method takes just one parameter key_element that refers to
2 min read
IdentityHashMap containsKey() Method in Java
The java.util.IdentityHashMap.containsKey() method is used to check whether a particular key is being mapped into the IdentityHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: Identity_HashMap.containsKey(key_element) Parameters: The method takes just one parameter key_element tha
2 min read
ConcurrentHashMap containsKey() Method in Java
The containsKey() method in Java's ConcurrentHashMap class is used to determine whether the map contains a given key. It has the following signature: boolean containsKey(Object key) where: key is the key to be searched for in the map. The containsKey() method works in a concurrent environment, which means that it can be called from multiple threads
3 min read
AbstractMap containsKey() Method in Java with Examples
The AbstractMap.containsKey() method is used to check whether a particular key is being mapped into the AbstractMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: AbstractMap.containsKey(key_element) Parameters: The method takes just one parameter key_element that refers to the key who
2 min read
Map containsKey() method in Java with Examples
The java.util.Map.containsKey() method is used to check whether a particular key is being mapped into the Map or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: boolean containsKey(key_element) Parameters: The method takes just one parameter key_element that refers to the key whose mapping
2 min read
ConcurrentSkipListMap containsKey() method in Java with Examples
The containsKey() method of java.util.concurrent.ConcurrentSkipListMap is an in-built function in Java which returns a true boolean value if the specified element is present in this map otherwise it returns false. Syntax: public boolean containsKey(Object ob) Parameter: The function accepts a single mandatory parameter ob which specifies the key wh
2 min read
Properties containsKey(value) method in Java with Examples
The containsKey(value) method of Properties class is used to check if this Properties object contains any mapping of this Key for any key present in it. It takes this value to be compared as parameter and returns a boolean value as result. Syntax: public Object containsKey(Object value) Parameters: This method accepts a parameter value which is the
2 min read
SimpleBindings containsKey() method in Java with Examples
The containsKey() method of SimpleBindings class is used to return true if this map which is backing this SimpleBindings object contains a mapping for the specified key. If SimpleBindings object contains this key then the method returns true else false. Syntax: public boolean containsKey(Object key) Parameters: This method accepts only one paramete
2 min read
HashMap clear() Method in Java
The java.util.HashMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified HashMap. Syntax: Hash_Map.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.HashMap.clear() Method:
2 min read
HashMap clone() Method in Java
Let us do clear out the foundation concept of shallow copy and deep copy in java. Shallow repetition is quicker. However, it’s “lazy” it handles pointers and references. Rather than creating a contemporary copy of the particular knowledge the pointer points to, it simply copies over the pointer price. So, each the first and therefore the copy can h
3 min read
HashMap containsValue() Method in Java
The java.util.HashMap.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the HashMap. It takes the Value as a parameter and returns True if that value is mapped by any of the key in the map. Syntax: Hash_Map.containsValue(Object Value) Parameters: The method takes just one paramete
2 min read
HashMap isEmpty() Method in Java
The java.util.HashMap.isEmpty() method of HashMap class is used to check for the emptiness of the map. The method returns True if no key-value pair or mapping is present in the map else False. Syntax: Hash_Map.isEmpty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the map is empty or does not
2 min read
HashMap entrySet() Method in Java
The java.util.HashMap.entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map or we can create a new set and store the map elements into them. Syntax: hash_map.entrySet() Parameters: The method does not take any parameter. Return Value: The method returns
2 min read
HashMap get() Method in Java
The java.util.HashMap.get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: Hash_Map.get(Object key_element) Parameter: The method takes one parameter key_element of object type and refers to the key whose
2 min read
HashMap keySet() Method in Java
The java.util.HashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: hash_map.keySet() Parameters: The method does not take any parameter. Return Value: The method returns a set havin
2 min read
HashMap size() Method in Java
The java.util.HashMap.size() method of HashMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. Syntax: Hash_Map.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the map which also means the number of key-value pairs present in t
2 min read
HashMap put() Method in Java
The java.util.HashMap.put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Syntax: Hash_Map.put
3 min read
HashMap putAll() Method in Java
The java.util.HashMap.putAll() is an inbuilt method of HashMap class that is used for the copy operation. The method copies all of the elements i.e., the mappings, from one map into another. Syntax: new_hash_map.putAll(exist_hash_map) Parameters: The method takes one parameter exist_hash_map that refers to the existing map we want to copy from. Ret
2 min read
HashMap remove() Method in Java
The java.util.HashMap.remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.Syntax: Hash_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map.Return Value: The
2 min read
HashMap values() Method in Java
The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The method is used to return a collection view containing all
2 min read
HashMap compute() method in Java with Examples
The compute(Key, BiFunction) method of HashMap class allows you to update a value in HashMap. The compute() method tries to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). This method is used to automatically update a value for given key in HashMap. If the remapping function pa
4 min read
HashMap computeIfAbsent() method in Java with Examples
The computeIfAbsent(Key, Function) method of HashMap class is used to compute the value for a given key using the given mapping function. Store the computed value for the key in Hashmap if the key is not already associated with a value (or is mapped to null) else null. Syntax: public V computeIfAbsent(K key, Function&lt;? super K, ? extends V&gt; r
2 min read
HashMap replace(key, oldValue, newValue) method in Java with Examples
The replace(K key, V oldValue, V newValue) method of Map interface, implemented by HashMap class is used to replace the old value of the specified key only if the key is previously mapped with the specified old value. Syntax: default boolean replace(K key, V oldValue, V newValue) Parameters: This method accepts three parameters : key: which is the
3 min read
HashMap replace(key, value) method in Java with Examples
The replace(K key, V value) method of Map interface, implemented by HashMap class is used to replace the value of the specified key only if the key is previously mapped with some value. Syntax: public V replace(K key, V value) Parameters: This method accepts two parameters: key: which is the key of the element whose value has to be replaced. value:
3 min read
three90RightbarBannerImg