java.util.WeakHashMap.remove() 方法
描述
remove(Object key) 方法用于从该映射中删除该键的映射(如果存在)。
声明
以下是 java.util.WeakHashMap.remove() 方法的声明。
public Object remove(Object key)
参数
key − 这是要从映射中删除其映射的键。
返回值
方法调用返回与该键关联的先前值,如果没有键映射,则返回 null。
异常
NA
示例
下面的例子展示了 java.util.WeakHashMap.remove() 方法的使用。
package com.tutorialspoint;
import java.util.Map;
import java.util.WeakHashMap;
public class WeakHashMapDemo {
public static void main(String[] args) {
Map<String, String> weakHashMap = new WeakHashMap<String, String>();
// put keys and values in the Map
System.out.println("Putting values into the Map");
weakHashMap.put("1", "first");
weakHashMap.put("2", "two");
weakHashMap.put("3", "three");
// checking Map
System.out.println("Map: "+weakHashMap);
// remove value at key 2
System.out.println("Returned value: "+weakHashMap.remove("2"));
System.out.println("New Map: "+weakHashMap);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Putting values into the Map Map: {1=first, 2=two, 3=three} Returned value: two New Map: {1=first, 3=three}