當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。