SimpleBindings putAll() method in Java with Examples

The putAll() method of SimpleBindings class is used to add all the key-value pairs of the map which is passed as a parameter to SimpleBindings object.

Synatx:

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

parameters: This method accepts parameter toMerge which is the Map of values to add.



Return Value: This method returns nothing.

Exception: This method throws following exceptions:

  • NullPointerException: if toMerge map is null or if some key in the map is null.
  • IllegalArgumentException: if some key in the map is an empty String.

Below are programs to Illustrate the working of putAll() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Key1:value1
Key2:value2
Key3:value3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Team1: India
Team2: Sri Lanka
Team3: Pakistan
Team4: Bangladesh

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

java-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.