Home » Java programming language

Java Dictionary remove() Method with Example

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

Dictionary Class remove() method

  • remove() method is available in java.util package.
  • remove() method is used to remove the key-value pairs from this dictionary.
  • remove() method is a non-static method, so it is accessible with the class object 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 key_value pairs.
    NullPointerException: This exception may throw when the given parameter is null exists.

Syntax:

    public abstract Value remove(Object key_ele);

Parameter(s):

  • Object key_ele – represents the key element to be removed from this dictionary.

Return value:

The return type of this method is Value, it returns value associated with the given key in this dictionary.

Example:

// Java program is to demonstrate the example of
// remove() method of Dictionary

import java.util.*;

public class RemoveOfDictionary {
    public static void main(String[] args) {
        // Instantiate a Hashtable object
        Dictionary dictionary = new Hashtable();

        // By using put() method is to
        // add an elements in Hashtable 
        dictionary.put(1, "C");
        dictionary.put(2, "C++");
        dictionary.put(3, "JAVA");
        dictionary.put(4, "PHP");
        dictionary.put(5, "SFDC");

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

        // Here we are removing the key-value pairs 
        // exists on the given key element 4 in this
        // dictionary by using remove() method
        dictionary.remove(4);

        // Display Modified Dictionary
        System.out.println("dictionary: " + dictionary);
    }
}

Output

dictionary: {5=SFDC, 4=PHP, 3=JAVA, 2=C++, 1=C}
dictionary: {5=SFDC, 3=JAVA, 2=C++, 1=C}


Comments and Discussions!

Load comments ↻





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