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


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


SimpleBindings类的get()方法用于返回此SimpleBindings对象将作为参数传递的键映射到的值。如果SimpleBindings对象不包含此键的映射,则方法返回null。

用法:

public Object get(Object key)

参数:此方法仅接受一个参数key,这是要返回其关联值的key。


返回值:此方法返回此键映射到的值,或者为null。

异常:此方法引发以下异常:

  • NullPointerException :如果键为空
  • ClassCastException:如果键不是字符串
  • IllegalArgumentException:如果键为空String

Java程序来说明get()方法的用法:

程序1:

// Java program to demonstrate get() 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"); 
  
        // get value of key3 
        Object value = bindings.get("key3"); 
  
        // print 
        System.out.println("key3 value:"
                           + value); 
    } 
}
输出:
key3 value:value3

程序2:

// Java program to demonstrate get() 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"); 
  
        // get team1, team2 and team5 values 
        Object team1 = asiaTeamList.get("team1"); 
        Object team2 = asiaTeamList.get("team2"); 
        Object team5 = asiaTeamList.get("team5"); 
  
        // print team names 
        System.out.println("Team1:"
                           + team1); 
        System.out.println("Team2:"
                           + team2); 
        System.out.println("Team5:"
                           + team5); 
    } 
}
输出:
Team1:India
Team2:Sri Lanka
Team5:null

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



相关用法


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