如果支持此SimpleBindings對象的映射包含指定鍵的映射,則SimpleBindings類的containsKey()方法用於返回true。如果SimpleBindings對象包含此鍵,則該方法返回true,否則返回false。
用法:
public boolean containsKey(Object key)
參數:此方法僅接受一個參數key,這是要在此映射中進行測試的鍵。
返回值:如果此映射包含指定鍵的映射,則此方法返回true。
異常:此方法引發以下異常:
- NullPointerException :如果鍵為空
- ClassCastException:如果鍵不是字符串
- IllegalArgumentException:如果鍵為空String
Java程序來說明containsKey()方法的用法:
程序1:
// 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);
}
}
輸出:
Key2 exits:true
程序2:
// 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);
}
}
輸出:
England exits in Asia Team:false
相關用法
- Java SimpleBindings put()用法及代碼示例
- Java SimpleBindings get()用法及代碼示例
- Java SimpleBindings putAll()用法及代碼示例
- Java SimpleBindings remove()用法及代碼示例
- Java Map containsKey()用法及代碼示例
- Java Properties containsKey(value)用法及代碼示例
- Java AbstractMap containsKey()用法及代碼示例
- Java ConcurrentSkipListMap containsKey()用法及代碼示例
- Java EnumMap containsKey()用法及代碼示例
- Java Hashtable containsKey()用法及代碼示例
- Java TreeMap containsKey()用法及代碼示例
- Java WeakHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java LinkedHashMap containsKey()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 SimpleBindings containsKey() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。