java.util.Hashtable.remove() Method


Description

The remove(Object key) method is used to remove the key (and its corresponding value) from this hashtable.

Declaration

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

public V remove(Object key)

Parameters

key − This is the key that needs to be removed.

Return Value

The method call returns the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping.

Exception

NullPointerException − This is thrown if the specified key is null.

Example

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

package com.tutorialspoint;

import java.util.*;

public class HashTableDemo {
   public static void main(String args[]) {
      
      // create hash table 
      Hashtable htable = new Hashtable(3); 

      // populate the table
      htable.put(1, "TP");
      htable.put(2, "IS");
      htable.put(3, "THE");
      htable.put(4, "BEST");
      htable.put(5, "TUTORIAL");

      System.out.println("Initial hash table value: "+htable);

      // remove element at key 3
      htable.remove(3);

      System.out.println("Hash table value after remove: "+htable);
   }    
}

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

Initial hash table value: {5=TUTORIAL, 4=BEST, 3=THE, 2=IS, 1=TP}
Hash table value after remove: {5=TUTORIAL, 4=BEST, 2=IS, 1=TP}
java_util_hashtable.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements