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


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


remove(Object key)

Java中的ConcurrentHashmap類的remove(Object key)方法用於從映射中刪除映射。如果鍵在Map中不存在,則此函數不執行任何操作。

句法

public V remove(Object key)

參數:此方法接受必需的參數key,這是需要刪除的key


返回值:此方法返回與key關聯的先前值;如果沒有key映射,則返回null。

異常:如果指定的鍵為null,則此方法將引發NullPointerException。

下麵是說明remove()方法的程序:

示例1:

// Java program to demonstrate 
// remove() method 
  
import java.util.concurrent.ConcurrentHashMap; 
import java.util.*; 
  
public class ConcurrentHashMapExample { 
  
    public static void main(String[] args) 
    { 
        // Creating ConcurrentHashMap 
        Map<String, String> 
            my_cmmap = new ConcurrentHashMap<String, String>(); 
  
        // Adding elements to the map 
        // using put() method 
        my_cmmap.put("1", "1"); 
        my_cmmap.put("2", "1"); 
        my_cmmap.put("3", "1"); 
        my_cmmap.put("4", "1"); 
        my_cmmap.put("5", "1"); 
        my_cmmap.put("6", "1"); 
  
        // Printing the map 
        System.out.println("Map: " + my_cmmap); 
        System.out.println(); 
  
        // Removing the mapping 
        // with existing key 6 
        // using remove() method 
        String valueRemoved = my_cmmap.remove("6"); 
  
        // Printing the map after remove() 
        System.out.println("After removing mapping with key 6:"); 
        System.out.println("Map: " + my_cmmap); 
        System.out.println("Value removed: " + valueRemoved); 
        System.out.println(); 
  
        // Removing the mapping 
        // with non-existing key 10 
        // using remove() method 
        valueRemoved = my_cmmap.remove("10"); 
  
        // Printing the map after remove() 
        System.out.println("After removing mapping with key 10:"); 
        System.out.println("Map: " + my_cmmap); 
        System.out.println("Value removed: " + valueRemoved); 
        System.out.println(); 
    } 
}
輸出:
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}

After removing mapping with key 6:
Map: {1=1, 2=1, 3=1, 4=1, 5=1}
Value removed: 1

After removing mapping with key 10:
Map: {1=1, 2=1, 3=1, 4=1, 5=1}
Value removed: null

示例2:演示NullPointerException

// Java program to demonstrate 
// remove() method 
  
import java.util.concurrent.ConcurrentHashMap; 
import java.util.*; 
  
public class ConcurrentHashMapExample { 
  
    public static void main(String[] args) 
    { 
        // Creating ConcurrentHashMap 
        Map<String, String> 
            my_cmmap = new ConcurrentHashMap<String, String>(); 
  
        // Adding elements to the map 
        // using put() method 
        my_cmmap.put("1", "1"); 
        my_cmmap.put("2", "1"); 
        my_cmmap.put("3", "1"); 
        my_cmmap.put("4", "1"); 
        my_cmmap.put("5", "1"); 
        my_cmmap.put("6", "1"); 
  
        // Printing the map 
        System.out.println("Map: " + my_cmmap); 
        System.out.println(); 
  
        try { 
            // Removing the mapping with null key 
            // using remove() method 
            my_cmmap.remove(null); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}

Exception: java.lang.NullPointerException

remove(Object key, Object value)

Java中的ConcurrentHashmap類的remove(Object key,Object value)方法用於從映射中刪除映射。在映射中搜索具有指定(鍵,值)對的映射,並刪除(如果找到)並返回true。如果鍵在映射中不存在,則此函數不執行任何操作並返回false。

用法:

public boolean remove(Object key, Object value)

參數:該方法接受兩個參數


  • key–與指定值關聯的鍵。
  • value–預期與指定鍵關聯的值。

返回值:如果刪除了該值,則此方法返回true。否則返回false。

異常:如果指定的鍵為null,則此方法將引發NullPointerException。

下麵是說明remove()方法的程序:

示例1:

// Java program to demonstrate 
// remove() method 
  
import java.util.concurrent.ConcurrentHashMap; 
import java.util.*; 
  
public class ConcurrentHashMapExample { 
  
    public static void main(String[] args) 
    { 
        // Creating ConcurrentHashMap 
        Map<String, String> 
            my_cmmap = new ConcurrentHashMap<String, String>(); 
  
        // Adding elements to the map 
        // using put() method 
        my_cmmap.put("1", "1"); 
        my_cmmap.put("2", "1"); 
        my_cmmap.put("3", "1"); 
        my_cmmap.put("4", "1"); 
        my_cmmap.put("5", "1"); 
        my_cmmap.put("6", "1"); 
  
        // Printing the map 
        System.out.println("Map: " + my_cmmap); 
        System.out.println(); 
  
        // Removing the mapping 
        // with existing pair 6, 1 
        // using remove() method 
        boolean valueRemoved = my_cmmap.remove("6", "1"); 
  
        // Printing the map after remove() 
        System.out.println("After removing mapping with pair (6, 1):"); 
        System.out.println("Map: " + my_cmmap); 
        System.out.println("Was value removed: " + valueRemoved); 
        System.out.println(); 
  
        // Removing the mapping 
        // with non-existing pair 1, 2 
        // using remove() method 
        valueRemoved = my_cmmap.remove("1", "2"); 
  
        // Printing the map after remove() 
        System.out.println("After removing mapping with pair (1, 2):"); 
        System.out.println("Map: " + my_cmmap); 
        System.out.println("Was value removed: " + valueRemoved); 
        System.out.println(); 
    } 
}
輸出:
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}

After removing mapping with pair (6, 1):
Map: {1=1, 2=1, 3=1, 4=1, 5=1}
Was value removed: true

After removing mapping with pair (1, 2):
Map: {1=1, 2=1, 3=1, 4=1, 5=1}
Was value removed: false

示例2:演示NullPointerException

// Java program to demonstrate 
// remove() method 
  
import java.util.concurrent.ConcurrentHashMap; 
import java.util.*; 
  
public class ConcurrentHashMapExample { 
  
    public static void main(String[] args) 
    { 
        // Creating ConcurrentHashMap 
        Map<String, String> 
            my_cmmap = new ConcurrentHashMap<String, String>(); 
  
        // Adding elements to the map 
        // using put() method 
        my_cmmap.put("1", "1"); 
        my_cmmap.put("2", "1"); 
        my_cmmap.put("3", "1"); 
        my_cmmap.put("4", "1"); 
        my_cmmap.put("5", "1"); 
        my_cmmap.put("6", "1"); 
  
        // Printing the map 
        System.out.println("Map: " + my_cmmap); 
        System.out.println(); 
  
        try { 
            // Removing the mapping 
            // with null key 
            // using remove() method 
            my_cmmap.remove(null, "1"); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}

Exception: java.lang.NullPointerException


相關用法


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