WeakHashMap remove(Object) in Java

remove(Object): This method is available in java.util.WeakHashMap class of Java.

Syntax:

V java.util.WeakHashMap.remove(Object key)

This method takes one argument. This method removes the mapping for a key from this weak hash map if it is present.

Parameters: One parameter is required for this method.

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.

Exceptions: NA

Approach

Java

import java.util.WeakHashMap;

public class WeakHashMapremove {
    public static void main(String[] args) {

        WeakHashMap<String, Integer> weakHashMap =
new WeakHashMap<String, Integer>();

        weakHashMap.put("Java", 1);
        weakHashMap.put("Hello", 2);

        Object key = "Java";

        System.out.println(weakHashMap.remove(key));

    }
}

Output:

1


No comments:

Post a Comment