SimpleBindings get() method in Java with Examples
Last Updated :
15 Feb, 2020
The get() method of SimpleBindings class is used to return the value to which this SimpleBindings object maps the key passed as parameter.if the SimpleBindings object contains no mapping for this key then method return null.
Syntax:
public Object get(Object key)
Parameters: This method accepts only one parameters key which is key whose associated value is to be returned.
Return Value: This method returns the value to which this key is mapped, or null.
Exception: This method throws following exceptions:
- NullPointerException: if key is null
- ClassCastException: if key is not String
- IllegalArgumentException: if key is empty String
Java programs to Illustrate the working of get() method:
Program 1:
import javax.script.SimpleBindings;
public class GFG {
public static void main(String[] args)
{
SimpleBindings bindings
= new SimpleBindings();
bindings.put( "key1" , "value1" );
bindings.put( "key2" , "value2" );
bindings.put( "key3" , "value3" );
Object value = bindings.get( "key3" );
System.out.println( "key3 value: "
+ value);
}
}
|
Output:
key3 value: value3
Program 2:
import javax.script.SimpleBindings;
public class GFG {
public static void main(String[] args)
{
SimpleBindings asiaTeamList
= new SimpleBindings();
asiaTeamList.put( "team1" , "India" );
asiaTeamList.put( "team2" , "Sri Lanka" );
asiaTeamList.put( "team3" , "Pakistan" );
asiaTeamList.put( "team4" , "Bangladesh" );
Object team1 = asiaTeamList.get( "team1" );
Object team2 = asiaTeamList.get( "team2" );
Object team5 = asiaTeamList.get( "team5" );
System.out.println( "Team1 :"
+ team1);
System.out.println( "Team2 :"
+ team2);
System.out.println( "Team5 :"
+ team5);
}
}
|
Output:
Team1 :India
Team2 :Sri Lanka
Team5 :null
References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#get(java.lang.Object)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...