Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
map
mapping
key
communitycreator

What is AbstractMap.containsKey() in Java?

Hammad Nasir

AbstractMap.containsKey() in Java is a method of the AbstractMap class. The method is used to check whether a key is mapped into the AbstractMap or not.

Syntax


public boolean containsKey(Object key)

Parameters

The function takes in one parameter.

  • key: the key whose mapping is to be examined inside a map.

Return value

  • The function returns a Boolean value.

  • The function returns true if AbstractMap includes a mapping for key.

  • Otherwise, false is returned.

Code

import java.util.*;
  
public class Abstract_Map_Class{
    public static void main(String[] args)
    {
        // empty AbstractMap is created
        AbstractMap<Integer, String> temp_absMap1 = new TreeMap<Integer, String>();
    
        // populating temp_absMap1
        temp_absMap1.put(1, "Hello");
        temp_absMap1.put(2, "from");
        temp_absMap1.put(3, "Educative");
        
        System.out.println("Displaying temp_absMap1 : " + temp_absMap1);
        
        System.out.println("** Checking temps_absMap1 to see if key 1 is present **"); 
        System.out.println("Result : "  + temp_absMap1.containsKey(1));
        
        System.out.println("** Checking temps_absMap1 to see if key 4 is present **"); 
        System.out.println("Result : "  + temp_absMap1.containsKey(4));
    }
}

RELATED TAGS

java
map
mapping
key
communitycreator
RELATED COURSES

View all Courses

Keep Exploring