Java.util.Dictionary.remove() Method


Advertisements


Description

The java.util.Dictionary.remove(Object key) method removes the value and it's corresponding key from this dictionary.

Declaration

Following is the declaration for java.util.Dictionary.remove() method

public abstract V remove(Object key)

Parameters

key − the key to be removed.

Return Value

This method returns the value to which the key had been mapped, or null if the key did not have a mapping.

Exception

NullPointerException − if key is null.

Example

The following example shows the usage of java.util.Dictionary.remove() method.

Live Demo
package com.tutorialspoint;

import java.util.*;

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

      // create new hashtable
      Dictionary d = new Hashtable();

      // add some elements
      d.put("1", "Chocolate");
      d.put("2", "Cocoa");
      d.put("5", "Coffee");

      // remove one element
      System.out.println(d.get("5"));
      System.out.println(d.remove("5") + " has been removed");
      System.out.println(d.get("5"));
   }
}

Let us compile and run the above program, this will produce the following result −

Coffee
Coffee has been removed
null

java_util_dictionary.htm

Advertisements