Java Utility Library

Java Dictionary - remove() Method



The java.util.Dictionary.remove() method is used to remove the key (and its corresponding value) from this dictionary. This method does nothing if the key is not in this dictionary.

Syntax

public abstract V remove(Object key)

Here, V is the type of value maintained by the container.


Parameters

key Specify the key that needs to be removed.

Return Value

Returns the value to which the key had been mapped in this dictionary, or null if the key did not have a mapping.

Exception

Throws NullPointerException, if the key is null.

Example:

In the example below, the java.util.Dictionary.remove() method is used to remove the key (and its corresponding value) from the given dictionary.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a dictionary
    Dictionary<Integer, String> Dict = 
        new Hashtable<Integer, String>();

    //populating dictionary
    Dict.put(101, "John");
    Dict.put(102, "Marry");
    Dict.put(103, "Kim");
    Dict.put(104, "Sam");
    Dict.put(105, "Jo");

    //printing the content of the dictionary
    System.out.println("Dict contains: " + Dict);

    //removing key 103 and 104
    Dict.remove(103);
    Dict.remove(104);

    //printing the content of the dictionary
    System.out.println("Dict contains: " + Dict);
  }
}

The output of the above code will be:

Dict contains: {105=Jo, 104=Sam, 103=Kim, 102=Marry, 101=John}
Dict contains: {105=Jo, 102=Marry, 101=John}

❮ Java.util - Dictionary