Java中的 ConcurrentHashMap remove() 方法
A. 移除(对象键)
Java中 ConcurrentHashmap 类的remove(Object key)方法用于从映射中移除映射。如果映射中不存在该键,则此函数不执行任何操作。
句法:
public V remove(Object key)
参数:
This method accepts a mandatory parameter key which is the key that needs to be removed
返回值:
This method returns the previous value associated with key,
or null if there was no mapping for key.
例外:
This method throws NullPointerException if the specified key is null.
以下是说明 remove() 方法的程序:
方案一:
Java
// Java program to demonstrate remove() method
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
}
}
Java
// Java program to demonstrate remove() method
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
}
}
}
Java
// Java program to demonstrate remove() method
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
}
}
Java
// Java program to demonstrate
// remove(key, value) null key
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
}
}
}
Java
// Java program to demonstrate
// remove(key, value) null value
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
try {
// Removing the mapping with correct key and
// null value using remove() method
my_cmmap.remove("1", null);
my_cmmap.remove("7", null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出
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:用 null 键演示 NullPointerException
Java
// Java program to demonstrate remove() method
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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
B.remove(对象键,对象值)
Java中ConcurrentHashmap类的remove(Object key, Object value)方法用于从map中移除映射。在映射中搜索具有指定(键,值)对的映射,如果找到则删除,并返回true。如果映射中不存在该键,则此函数不执行任何操作并返回 false。
句法:
public boolean remove(Object key, Object value)
参数:
This method accepts two parameters
key - key with which the specified value is associated.
value - value expected to be associated with the specified key.
返回值:
This method returns true if the value was removed. Else it returns false.
例外:
This method throws NullPointerException if the specified key is null.
以下是说明 remove() 方法的程序:
方案一:
Java
// Java program to demonstrate remove() method
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
}
}
输出
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:用 null 键演示 NullPointerException
Java
// Java program to demonstrate
// remove(key, value) null key
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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
程序 3 :用 null 值演示 NullPointerException
Java
// Java program to demonstrate
// remove(key, value) null value
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map my_cmmap
= new ConcurrentHashMap();
// 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);
try {
// Removing the mapping with correct key and
// null value using remove() method
my_cmmap.remove("1", null);
my_cmmap.remove("7", null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}