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


Java SimpleBindings putAll()用法及代碼示例


SimpleBindings類的putAll()方法用於添加作為參數傳遞給SimpleBindings對象的Map的所有鍵值對。

Synatx:

public void putAll(Map<String, Object> toMerge)

參數:此方法接受參數toMerge,這是要添加的值的映射。


返回值:此方法不返回任何內容。

異常:此方法引發以下異常:

  • NullPointerException :如果toMerge映射為null或映射中的某些鍵為null。
  • IllegalArgumentException:如果映射中的某些鍵為空字符串。

下麵是說明putAll()方法工作方式的程序:

程序1:

// Java programs to Illustrate 
// the working of putAll() method 
  
import javax.script.SimpleBindings; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create simpleBindings object 
        SimpleBindings bindings = new SimpleBindings(); 
  
        // create Map 
        HashMap<String, String> map = new HashMap<>(); 
  
        // add key value pair to Map 
        map.put("key1", "value1"); 
        map.put("key2", "value2"); 
        map.put("key3", "value3"); 
  
        // apply putAll 
        bindings.putAll(map); 
  
        // print 
        System.out.println( 
            "Key1:"
            + bindings.get("key1")); 
        System.out.println( 
            "Key2:"
            + bindings.get("key2")); 
        System.out.println( 
            "Key3:"
            + bindings.get("key3")); 
    } 
}
輸出:
Key1:value1
Key2:value2
Key3:value3

程序2:

// Java programs to Illustrate 
// the working of putAll() method 
  
import javax.script.SimpleBindings; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create simpleBindings object 
        SimpleBindings asiaTeamList 
            = new SimpleBindings(); 
  
        // create Map 
        HashMap<String, String> map = new HashMap<>(); 
  
        // add team in map using put() 
        map.put("team1", "India"); 
        map.put("team2", "Sri Lanka"); 
        map.put("team3", "Pakistan"); 
        map.put("team4", "Bangladesh"); 
  
        // apply putAll 
        asiaTeamList.putAll(map); 
  
        // print 
        System.out.println( 
            "Team1:"
            + asiaTeamList.get("team1")); 
        System.out.println( 
            "Team2:"
            + asiaTeamList.get("team2")); 
        System.out.println( 
            "Team3:"
            + asiaTeamList.get("team3")); 
        System.out.println( 
            "Team4:"
            + asiaTeamList.get("team4")); 
    } 
}
輸出:
Team1:India
Team2:Sri Lanka
Team3:Pakistan
Team4:Bangladesh

參考文獻: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#putAll(java.util.Map)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 SimpleBindings putAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。