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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。