Home » Java programming language

Java Hashtable remove() Method with Example

Hashtable Class remove() method: Here, we are going to learn about the remove() method of Hashtable Class with its syntax and example.
Submitted by Preeti Jain, on February 17, 2020

Hashtable Class remove() method

  • remove() method is available in java.util package.
  • remove() method is used to delete or remove the given key element (key_ele) and its desired value got deleted automatically from this Hashtable.
  • remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • remove() method may throw an exception at the time of removing the given key element (key_ele).
    NullPointerException: This exception may throw when the given key element (key_ele) is null exists.

Syntax:

    public Value remove(Object key_ele);

Parameter(s):

  • Object key_ele – represents the key element (key_ele) is to be removed when exists.

Return value:

The return type of the method is Value, it returns the value element associated with the given key element (key-ele) when exists otherwise it returns null.

Example:

// Java program to demonstrate the example 
// of Value remove(Object key_ele) method 
// of Hashtable 

import java.util.*;

public class RemoveOfHashtable {
    public static void main(String[] args) {
        // Instantiate a hashtable object  
        Hashtable ht = new Hashtable();

        // By using put() method is to
        // add the linked values in an
        // Hashtable ht
        ht.put(10, "C");
        ht.put(20, "C++");
        ht.put(30, "JAVA");
        ht.put(40, "PHP");
        ht.put(50, "SFDC");

        // Display Hashtable
        System.out.println("Hashtable :" + ht);

        // By using remove() method is to remove
        // the key-value pairs exists on the given
        // key element in this Hashtable
        ht.remove(40);
        System.out.println("ht.remove(40) :" + ht);
    }
}

Output

Hashtable :{10=C, 20=C++, 30=JAVA, 40=PHP, 50=SFDC}
ht.remove(40) :{10=C, 20=C++, 30=JAVA, 50=SFDC}


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.