SimpleBindings get() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate get() method
  
import javax.script.SimpleBindings;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create simpleBindings object
        SimpleBindings bindings
            = new SimpleBindings();
  
        // add some keys with values
        bindings.put("key1", "value1");
        bindings.put("key2", "value2");
        bindings.put("key3", "value3");
  
        // get value of key3
        Object value = bindings.get("key3");
  
        // print
        System.out.println("key3 value: "
                           + value);
    }
}

chevron_right


Output:

key3 value: value3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate get() method
  
import javax.script.SimpleBindings;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create simpleBindings object
        SimpleBindings asiaTeamList
            = new SimpleBindings();
  
        // add team in asiaTeamList
        asiaTeamList.put("team1", "India");
        asiaTeamList.put("team2", "Sri Lanka");
        asiaTeamList.put("team3", "Pakistan");
        asiaTeamList.put("team4", "Bangladesh");
  
        // get team1, team2 and team5 values
        Object team1 = asiaTeamList.get("team1");
        Object team2 = asiaTeamList.get("team2");
        Object team5 = asiaTeamList.get("team5");
  
        // print team names
        System.out.println("Team1 :"
                           + team1);
        System.out.println("Team2 :"
                           + team2);
        System.out.println("Team5 :"
                           + team5);
    }
}

chevron_right


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)

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.