LinkedHashMap remove(Object) in Java

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

Syntax:

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

This method takes one argument. This method removes the mapping for the specified key from this map if 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.LinkedHashMap;

public class LinkedHashMapremove {

    public static void main(String args[]) {

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

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

        Object key = "Hello";

        System.out.println(linkedHashMap.remove(key));
    }

}

Output:

2


No comments:

Post a Comment