SimpleBindings putAll() method in Java with Examples
Last Updated :
13 Aug, 2021
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
import javax.script.SimpleBindings;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
SimpleBindings bindings = new SimpleBindings();
HashMap<String, String> map = new HashMap<>();
map.put( "key1" , "value1" );
map.put( "key2" , "value2" );
map.put( "key3" , "value3" );
bindings.putAll(map);
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
import javax.script.SimpleBindings;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
SimpleBindings asiaTeamList
= new SimpleBindings();
HashMap<String, String> map = new HashMap<>();
map.put( "team1" , "India" );
map.put( "team2" , "Sri Lanka" );
map.put( "team3" , "Pakistan" );
map.put( "team4" , "Bangladesh" );
asiaTeamList.putAll(map);
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...