Open In App

SimpleBindings remove() method in Java with Examples

Last Updated : 22 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The remove() method of SimpleBindings class is used to remove the mapping for this key from this SimpleBindings object if it is present. This method returns the previous value associated with the specified key, or null if there was no mapping for key. 
Syntax: 
 

public Object remove(Object key)

Parameters: This method accepts one parameter key which is key whose mapping is to be removed from the map.
Return Value: This method returns the previous value associated with the specified key, or null if there was no mapping for key.
Exception: This method throws following exceptions: 
 

  • NullPointerException: if key is null.
  • ClassCastException: if key is not String.
  • IllegalArgumentException: if key is empty String.

Below are programs to Illustrate the working of remove() method:
Example 1: 
 

Java




// Java programs to Illustrate
// the working of remove() method
 
import javax.script.SimpleBindings;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create simpleBindings object
        SimpleBindings bindings = new SimpleBindings();
 
        // add key value pair using remove()
        bindings.put("key1", "value1");
        bindings.put("key2", "value2");
        bindings.put("key3", "value3");
 
        // print before removing key1 and key2
        System.out.println("before removing key1 and key2");
        System.out.println("Key1: " + bindings.get("key1"));
        System.out.println("Key2: " + bindings.get("key2"));
        System.out.println("Key3: " + bindings.get("key3"));
 
        // remove key1 and key2
        bindings.remove("key1");
        bindings.remove("key2");
 
        // print after removing key1 and key2
        System.out.println("after removing key1 and key2");
        System.out.println("Key1: " + bindings.get("key1"));
        System.out.println("Key2: " + bindings.get("key2"));
        System.out.println("Key3: " + bindings.get("key3"));
    }
}


Output: 

before removing key1 and key2
Key1: value1
Key2: value2
Key3: value3
after removing key1 and key2
Key1: null
Key2: null
Key3: value3

 

Example 2: 
 

Java




// Java programs to Illustrate
// the working of remove() 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 using remove()
        asiaTeamList.put("team1", "India");
        asiaTeamList.put("team2", "Sri Lanka");
        asiaTeamList.put("team3", "Pakistan");
        asiaTeamList.put("team4", "Bangladesh");
 
        // print before removing
        System.out.println("before removing team3 and team4");
        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"));
 
        // remove team3 and team4
        asiaTeamList.remove("team3");
        asiaTeamList.remove("team4");
 
        // print before removing key1 and key2
        System.out.println("after removing team3 and team4");
        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: 

before removing team3 and team4
Team1: India
Team2: Sri Lanka
Team3: Pakistan
Team4: Bangladesh
after removing team3 and team4
Team1: India
Team2: Sri Lanka
Team3: null
Team4: null

 

References: https: //docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#remove(java.lang.String, java.lang.Object)
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads