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:
Java
import javax.script.SimpleBindings;
public class GFG {
public static void main(String[] args)
{
SimpleBindings bindings = new SimpleBindings();
bindings.put("key1", "value1");
bindings.put("key2", "value2");
bindings.put("key3", "value3");
boolean isKey2Exist = bindings.containsKey("key2");
System.out.println("Key2 exists:" + isKey2Exist);
}
}
|
Output:
key2 exists:true
Program 2:
Java
import javax.script.SimpleBindings;
public class GFG {
public static void main(String[] args)
{
SimpleBindings asiaTeamList
= new SimpleBindings();
asiaTeamList.put("team1", "India");
asiaTeamList.put("team2", "Sri Lanka");
asiaTeamList.put("team3", "Pakistan");
asiaTeamList.put("team4", "Bangladesh");
boolean isKey2Exist
= asiaTeamList.containsKey("England");
System.out.println("England exists in Asia Team:"
+ isKey2Exist);
}
}
|
Output:
England exists in Asia Team:false
References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#containsKey(java.lang.Object)
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...