HashMap containsKey(Object) in Java

containsKey(Object): This method is available in java.util.HashMap class of Java.

Syntax:

boolean java.util.HashMap.containsKey(Object key)

This method takes one argument. This method returns true if this map contains a mapping for the specified key.

Parameters: One parameter is required for this method.

key: The key whose presence in this map is to be tested.

Returns: true if this map contains a mapping for the specified key.

Exceptions: NA

Approach

Java

import java.util.HashMap;

public class HashMapcontainsKey {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap =
new HashMap<String, Integer>();

        hashMap.put("Hello", 1);
        hashMap.put("World", 2);

        String key = "Hello";

        System.out.println(hashMap.containsKey(key));

    }
}

Output:

true


No comments:

Post a Comment