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 parameters key which is key whose presence in this map is to be tested.

Return Value: This method returns true if this map contains a mapping for the specified key.

Exception: This method throws following exceptions:



  • NullPointerException: if key is null
  • ClassCastException: if key is not String
  • IllegalArgumentException: if key is empty String

Java programs to Illustrate the working of containsKey() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate containsKey() method
  
import javax.script.SimpleBindings;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create simpleBindings object
        SimpleBindings bindings = new SimpleBindings();
  
        // add some keys with values
        bindings.put("key1", "value1");
        bindings.put("key2", "value2");
        bindings.put("key3", "value3");
  
        // check key2 is exists in bindings or not
        boolean isKey2Exist = bindings.containsKey("key2");
  
        // print
        System.out.println("Key2 exits:" + isKey2Exist);
    }
}

chevron_right


Output:

Key2 exits:true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate containsKey() method
  
import javax.script.SimpleBindings;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create simpleBindings object
        SimpleBindings asiaTeamList
            = new SimpleBindings();
  
        // add team in asiaTeamList
        asiaTeamList.put("team1", "India");
        asiaTeamList.put("team2", "Sri Lanka");
        asiaTeamList.put("team3", "Pakistan");
        asiaTeamList.put("team4", "Bangladesh");
  
        // check England exists in bindings or not
        boolean isKey2Exist
            = asiaTeamList.containsKey("England");
  
        // print
        System.out.println("England exits in Asia Team:"
                           + isKey2Exist);
    }
}

chevron_right


Output:

England exits in Asia Team:false

References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#containsKey(java.lang.Object)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.