📜  Java中的 SimpleBindings putAll() 方法及示例

📅  最后修改于: 2022-05-13 01:55:34.515000             🧑  作者: Mango

Java中的 SimpleBindings putAll() 方法及示例

SimpleBindings 类putAll()方法用于添加作为参数传递给 SimpleBindings 对象的映射的所有键值对。

句法:

public void putAll(Map toMerge)

参数:此方法接受参数toMerge ,这是要添加的值的 Map。
返回值:此方法不返回任何内容。
异常:此方法抛出以下异常:

  • NullPointerException : 如果 toMerge 映射为空或者映射中的某个键为空。
  • IllegalArgumentException :如果地图中的某个键是空字符串。

以下是说明 putAll() 方法工作的程序:
方案一:

Java
// 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 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"));
    }
}


Java
// 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 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"));
    }
}


输出:
Key1:value1
Key2:value2
Key3:value3

方案二:

Java

// 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 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)