WeakHashMap remove(Object, Object) in Java

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

Syntax:

boolean java.util.Map.remove(Object key, Object value)

This method takes two arguments. This method removes the entry for the specified key only if it is currently mapped to the specified value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is associated.

value: value expected to be associated with the specified key.

Returns: true if the value was removed.

Throws:

1. UnsupportedOperationException - if the remove operation is not supported by this map.

2. ClassCastException - if the key or value is of an inappropriate type for this map.

3. NullPointerException - if the specified key or value is null, and this map does not permit null keys or values.

Approach

Java

import java.util.WeakHashMap;

public class WeakHashMapremove2 {
    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", value = 2;

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

    }
}

Output:

false


No comments:

Post a Comment