Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaSimpleBindings get() method in Java with Examples

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:




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


Output:

key3 value: value3

Program 2:




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


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)

RELATED ARTICLES

Most Popular

Recent Comments