当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java SimpleBindings remove()用法及代码示例


SimpleBindings类的remove()方法用于从此SimpleBindings对象中删除此键的映射(如果存在)。此方法返回与指定键关联的先前值;如果没有键的映射关系,则返回null。

Synatx:

public Object remove(Object key)

参数:此方法接受一个参数键,该参数键是要从映射中删除其映射的键。


返回值:此方法返回与指定键关联的先前值;如果没有键的映射关系,则返回null。

异常:此方法引发以下异常:

  • NullPointerException :如果key为null。
  • ClassCastException:if键不是String。
  • IllegalArgumentException:if键为空String。

下面是说明remove()方法工作方式的程序:

范例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

范例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

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



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 SimpleBindings remove() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。