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


Java ConcurrentSkipListMap remove()用法及代碼示例


java.util.concurrent.ConcurrentSkipListMap的remove()方法是Java中的內置函數,該函數從此映射中刪除指定鍵的映射。如果該特定鍵沒有映射,則該方法返回null。執行此方法後,將減小Map的大小。

用法:

ConcurrentSkipListMap.remove(Object ob)

參數:該函數接受單個強製性參數ob,它指定要刪除其映射的鍵。


返回值:該函數返回與指定鍵關聯的先前值;如果鍵沒有映射,則返回null。

以下示例程序旨在說明上述方法:

// Java Program Demonstrate remove() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // Adding elements to this map 
        for (int i = 1; i <= 5; i++) 
            mpp.put(i, i); 
        // remove() operation on the map 
        mpp.remove(1); 
  
        System.out.println("After remove(): " + mpp); 
    } 
}
輸出:
After remove(): {2=2, 3=3, 4=4, 5=5}

示例2:

// Java Program Demonstrate remove() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // Adding elements to this map 
        for (int i = 1; i <= 5; i++) 
            mpp.put(i, i); 
        // remove() operation on the map 
        mpp.remove(5); 
  
        System.out.println("After remove(): " + mpp); 
    } 
}
輸出:
After remove(): {1=1, 2=2, 3=3, 4=4}

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#remove-java.lang.Object-



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 ConcurrentSkipListMap remove() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。