LinkedHashMap remove(Object, Object) in Java

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

Syntax:

boolean java.util.HashMap.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.

Exceptions: NA

Approach

Java

import java.util.LinkedHashMap;

public class LinkedHashMapremove2 {

    public static void main(String args[]) {

        LinkedHashMap<String, Integer> linkedHashMap =
new LinkedHashMap<String, Integer>();

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

        Object key = "Java", value = 1;
        System.out.println(linkedHashMap.remove(key, value));

    }

}

Output:

true


No comments:

Post a Comment