當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。