Saturday, January 11, 2025
Google search engine
HomeLanguagesJavaSimpleBindings putAll() method in Java with Examples

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.
 

Syntax:  

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:  

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


Output: 

Key1:value1
Key2:value2
Key3:value3

 

Program 2: 

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


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)

RELATED ARTICLES

Most Popular

Recent Comments