当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java SimpleBindings containsKey()用法及代码示例


如果支持此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

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



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 SimpleBindings containsKey() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。