The remove() method of Dictionary Class accepts a key as a parameter and removes the corresponding value mapped to the key.
Syntax:
public abstract V remove(Object key)
Parameters: The function accepts a parameter key which denotes the key which is to be removed from the dictionary along with its mappings.
Return Values: The function returns the value which was mapped to the key or returns NULL if the key had no mapping.
Exception: The function throws no NullPointerException if the key passed as parameter is NULL.
Below programs illustrate the use of java.util.Dictionary.remove() method:
Program 1:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Dictionary<Integer, String>
d = new Hashtable<Integer, String>();
d.put( 1 , "Geeks" );
d.put( 2 , "for" );
d.put( 3 , "Geeks" );
System.out.println( "\nDictionary: " + d);
System.out.println(d.remove( 3 )
+ " has been removed" );
System.out.println( "\nDictionary: " + d);
}
}
|
Output:
Dictionary: {3=Geeks, 2=for, 1=Geeks}
Geeks has been removed
Dictionary: {2=for, 1=Geeks}
Program 2:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Dictionary<String, String> d = new Hashtable<String, String>();
d.put( "a" , "GFG" );
d.put( "b" , "gfg" );
System.out.println( "\nDictionary: " + d);
System.out.println(d.remove( "a" )
+ " has been removed" );
System.out.println(d.remove( "b" )
+ " has been removed" );
System.out.println( "\nDictionary: " + d);
if (d.isEmpty()) {
System.out.println( "Dictionary "
+ "is empty" );
}
else
System.out.println( "Dictionary "
+ "is not empty" );
}
}
|
Output:
Dictionary: {b=gfg, a=GFG}
GFG has been removed
gfg has been removed
Dictionary: {}
Dictionary is empty
Reference:https://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html#remove()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Oct, 2018
Like Article
Save Article