java.util.WeakHashMap.remove()是WeakHashMap类的内置方法,用于从映射中删除任何特定键的映射。它本质上删除了Map中任何特定键的值。
用法:
Weak_Hash_Map.remove(Object key)
参数:该方法采用一个参数键,该键的映射关系将从Map中删除。
返回值:如果该键存在,则该方法返回先前映射到指定键的值,否则该方法返回NULL。
以下示例程序旨在说明java.util.WeakHashMap.remove()方法的用法:
示例1:传递现有 key 时。
// Java code to illustrate the remove() method
import java.util.*;
public class Weak_Hash_Map_Demo {
public static void main(String[] args)
{
// Creating an empty WeakHashMap
Map<Integer, String> weak_hash = new
WeakHashMap<Integer, String>();
// Mapping string values to int keys
weak_hash.put(10, "Geeks");
weak_hash.put(15, "4");
weak_hash.put(20, "Geeks");
weak_hash.put(25, "Welcomes");
weak_hash.put(30, "You");
// Displaying the WeakHashMap
System.out.println("Initial Mappings are: " +
weak_hash);
// Removing the existing key mapping
String returned_value =
(String)weak_hash.remove(20);
// Verifying the returned value
System.out.println("Returned value is: " +
returned_value);
// Displayin the new map
System.out.println("New map is: " + weak_hash);
}
}
输出:
Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks} Returned value is: Geeks New map is: {30=You, 15=4, 10=Geeks, 25=Welcomes}
示例2:传递新 key 时。
// Java code to illustrate the remove() method
import java.util.*;
public class Weak_Hash_Map_Demo {
public static void main(String[] args)
{
// Creating an empty WeakHashMap
Map<Integer, String> weak_hash =
new WeakHashMap<Integer, String>();
// Mapping string values to int keys
weak_hash.put(10, "Geeks");
weak_hash.put(15, "4");
weak_hash.put(20, "Geeks");
weak_hash.put(25, "Welcomes");
weak_hash.put(30, "You");
// Displaying the WeakHashMap
System.out.println("Initial Mappings are: " +
weak_hash);
// Removing the new key mapping
String returned_value =
(String)weak_hash.remove(50);
// Verifying the returned value
System.out.println("Returned value is: " +
returned_value);
// Displayin the new map
System.out.println("New map is: "+weak_hash);
}
}
输出:
Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks} Returned value is: null New map is: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks}
注意:可以对具有不同数据类型的变化和组合的任何类型的映射执行相同的操作。
相关用法
- Java WeakHashMap get()用法及代码示例
- Java WeakHashMap put()用法及代码示例
- Java WeakHashMap keySet()用法及代码示例
- Java WeakHashMap putall()用法及代码示例
- Java WeakHashMap values()用法及代码示例
- Java WeakHashMap clear()用法及代码示例
- Java WeakHashMap containsKey()用法及代码示例
- Java WeakHashMap containsValue()用法及代码示例
- Java WeakHashMap entrySet()用法及代码示例
- Java WeakHashMap size()用法及代码示例
- Java WeakHashMap isEmpty()用法及代码示例
- Java WeakHashMap用法及代码示例
- Java TreeMap remove()用法及代码示例
- Java ConcurrentHashMap remove()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 WeakHashMap remove() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。