Removes the mapping for a key from this weak hash map if it is present. More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
Program
package com.candidjava;
import java.util.Map;
import java.util.WeakHashMap;
/*
* @author : Mohan
* @description : The remove(Object key) method is used to remove the mapping for this key from this map if present.
*/
public class WeakHashMapRemove {
public static void main(String[] args) {
Map<String, String> weakHashMap = new WeakHashMap<String, String>();
weakHashMap.put("1", "anandh");
weakHashMap.put("2", "hari");
weakHashMap.put("3", "vinoth");
weakHashMap.put("4", "kamal");
weakHashMap.put("5", "karthic");
weakHashMap.put("6", "mohan");
System.out.println("Map: " + weakHashMap);
System.out.println("Returned value: " + weakHashMap.remove("2"));
System.out.println("New Map: " + weakHashMap);
}
}
New Map: {4=kamal, 5=karthic, 6=mohan, 1=anandh, 3=vinoth}
Explanation
public V remove(Object key)
Removes the mapping for a key from this weak hash map if it is present. More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. A return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.
The map will not contain a mapping for the specified key once the call returns.
Specified by:
remove in interface Map
Overrides:
remove in class AbstractMap
Parameters:
key - key whose mapping is to be removed from the map
Returns:
the previous value associated with key, or null if there was no mapping for key