SimpleBindings remove() method in Java with Examples
source link: https://www.geeksforgeeks.org/simplebindings-remove-method-in-java-with-examples/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SimpleBindings remove() method in Java with Examples
- Last Updated : 22 Jul, 2021
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 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"
));
}
}
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 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"
));
}
}
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK