Java中的 SimpleBindings remove() 方法及示例
SimpleBindings 类的remove()方法用于从此 SimpleBindings 对象中删除此键的映射(如果存在)。此方法返回与指定键关联的先前值,如果键没有映射,则返回 null。
句法:
public Object remove(Object key)
参数:此方法接受一个参数键,该键是要从映射中删除其映射的键。
返回值:此方法返回与指定键关联的先前值,如果键没有映射,则返回 null。
异常:此方法抛出以下异常:
- NullPointerException : 如果键为空。
- ClassCastException :如果键不是字符串。
- IllegalArgumentException :如果键为空字符串。
以下是说明 remove() 方法工作的程序:
示例 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"));
}
}
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"));
}
}
输出:
before removing key1 and key2
Key1: value1
Key2: value2
Key3: value3
after removing key1 and key2
Key1: null
Key2: null
Key3: value3
示例 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"));
}
}
输出:
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
参考资料: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#remove(Java.lang.String, Java.lang.Object)