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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。