The Wayback Machine - https://web.archive.org/web/20240927030316/https://www.geeksforgeeks.org/hashtable-remove-method-in-java/
Open In App

Hashtable remove() Method in Java

Last Updated : 24 Jun, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The java.util.Hashtable.remove() is an inbuilt method of Hashtable class and is used to remove the mapping of any particular key from the table. It basically removes the values for any particular key in the Table.
Syntax: 
 

Hash_Table.remove(Object key)

Parameters: The method takes one parameter key whose mapping is to be removed from the Table.
Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL.
Below programs illustrates the working of java.util.Hashtable.remove() method: 
Program 1: When passing an existing key. 
 

Java




// Java code to illustrate the remove() method
import java.util.*;
 
public class Hash_Table_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty Hashtable
        Hashtable<Integer, String> hash_table =
        new Hashtable<Integer, String>();
 
        // Inserting elements into the table
        hash_table.put(10, "Geeks");
        hash_table.put(15, "4");
        hash_table.put(20, "Geeks");
        hash_table.put(25, "Welcomes");
        hash_table.put(30, "You");
 
        // Displaying the Hashtable
        System.out.println("Initial Table is: " + hash_table);
 
        // Removing the existing key mapping
        String returned_value = (String)hash_table.remove(20);
 
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
 
        // Displaying the new table
        System.out.println("New table is: " + hash_table);
    }
}


Output: 

Initial Table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: Geeks
New table is: {10=Geeks, 30=You, 15=4, 25=Welcomes}

 

Program 2: When passing a new key. 
 

Java




// Java code to illustrate the remove() method
import java.util.*;
 
public class Hash_Table_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty Hashtable
        Hashtable<Integer, String> hash_table =
        new Hashtable<Integer, String>();
 
        // Inserting mappings into the table
        hash_table.put(10, "Geeks");
        hash_table.put(15, "4");
        hash_table.put(20, "Geeks");
        hash_table.put(25, "Welcomes");
        hash_table.put(30, "You");
 
        // Displaying the Hashtable
        System.out.println("Initial table is: " + hash_table);
 
        // Removing the new key mapping
        String returned_value = (String)hash_table.remove(50);
 
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
 
        // Displaying the new table
        System.out.println("New table is: " + hash_table);
    }
}


Output: 

Initial table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: null
New table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}

 

Note: The same operation can be performed with any type of variation and combination of different data types.
 



Previous Article
Next Article

Similar Reads

Hashtable toString() Method in Java
The toString() method in the Hashtable class in Java returns a string representation of the key-value mappings contained in the hashtable. The string is in the form of a comma-separated list of key-value pairs enclosed in curly braces. The toString() method is inherited from the java.util.Hashtable class's parent class, java.util.Dictionary. Here's
2 min read
Hashtable elements() Method in Java
The elements() method in the Hashtable class in Java returns an enumeration of the values contained in the hashtable. The enumeration returned by this method traverses the hashtable's values in the order in which they were added to the hashtable. The elements() method is inherited from the java.util.Hashtable class's parent class, java.util.Diction
2 min read
Hashtable keys() Method in Java
As we all know enumeration defines java class type so do enumerations can have constructors, methods, and instance variables. The java.util.Hashtable.keys() method of Hashtable class in Java is used to get the enumeration of the keys present in the hashtable. Illustration: Syntax: public Enumeration<K> keys() Enumeration enu = Hash_table.keys
2 min read
Hashtable containsValue() Method in Java
The java.util.Hashtable.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Hashtable. It takes the Value as a parameter and returns True if that value is mapped by any of the keys in the table. Syntax: Hash_Table.containsValue(Object Value) Parameters: The method takes just one
2 min read
Hashtable put() Method in Java
The java.util.Hashtable.put() method of Hashtable is used to insert a mapping into a table. This means we can insert a specific key and the value it is mapping to into a particular table. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Syntax: Hash
3 min read
Hashtable get() Method in Java
The java.util.Hashtable.get() method of Hashtable class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the table contains no such mapping for the key. Syntax: Hash_Table.get(Object key_element) Parameter: The method takes one parameter key_element of object type and refers to the k
2 min read
Hashtable isEmpty() Method in Java
The java.util.Hashtable.isEmpty() method of Hashtable class is used to check for the emptiness of the table. The method returns True if no key-value pair or mapping is present in the table else False. Syntax: Hash_Table.isEmpty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the table is empty
2 min read
Hashtable size() Method in Java
The java.util.Hashtable.size() method of Hashtable class is used to get the size of the table which refers to the number of the key-value pair or mappings in the Table. Syntax: Hash_Table.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the table which also means the number of key-value pairs
2 min read
Hashtable clone() Method in Java
The java.util.Hashtable.clone() method is used to return a shallow copy of the mentioned hash table. It just creates a copy of the table. Syntax: Hash_Table.clone() Parameters: The method does not take any parameters. Return Value: The method just returns a copy of the Hashtable. Below programs are used to illustrate the working of java.util.Hashta
2 min read
Hashtable clear() Method in Java
The java.util.Hashtable.clear() method in Java is used to clear and remove all of the keys from a specified Hashtable. Syntax: Hash_table.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.Hashtable.clear() Method: Program
2 min read
Hashtable contains() Method in Java
The java.util.Hashtable.contains(Object value) method in Java is used to check whether a particular value is being mapped by any keys present in the Hashtable. Syntax: Hash_table.contains(Object value) Parameters: The method accepts one parameter value of object type and refers to the value of the hashtable whose mapping is to be verified. Return V
2 min read
Hashtable computeIfAbsent() method in Java with Examples
The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null). If mapping function of this method returns null, then no mapping is recorded. If the remapping function throws an exception, the exception is rethrown, an
2 min read
HashTable putIfAbsent() method in Java with Examples
The putIfAbsent(Key, value) method of Hashtable class which allows to map a value to a given key if given key is not associated with a value or mapped to null. A null value is returned if such key-value set is already present in the HashMap. Syntax: public V putIfAbsent(K key, V value) Parameters: This method accepts two parameters: key: specifies
2 min read
HashTable forEach() method in Java with Examples
The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. Method traverses each element of Hashtable until a
2 min read
Hashtable keySet() Method in Java with Examples
The java.util.Hashtable is used to create a set of key elements in the hash table. It basically returns a set view of the keys, or we can create a new set and store the key elements in them. Syntax: public Set<K> keySet() K : type of the Keys in the hash table Parameters: The method does not take any parameter. Return Value: The method return
2 min read
How HashTable Works Internally in Java?
Hashtable is a kind of Hash map but is synchronized. Hash map is non–synchronized, permits one null key & multiple null values, not-thread safe i.e. cannot share between many threads without proper synchronization, the key/values pairs are stored in Hashtable. The Hash table does not allow null value/key. When using this you specify an object t
11 min read
Difference Between Hashtable and Synchronized Map in Java
The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and also the equals method. Features of Hashtable: It is kind of like HashMap but is synchronized.Has
3 min read
Difference Between ConcurrentHashMap, HashTable and Synchronized Map in Java
Here we will be discussing out one by one individually then rolling onto the difference after having an adequate understanding of all three of them. We will be discussing ConcurrentHashMap first followed by HashTable, lastly followed by Synchronized Map. Let us start with ConcurrentHashMap first. The underlying data structure for ConcurrentHashMap
6 min read
Differences between HashMap and HashTable in Java
HashMap and Hashtable store key and value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. Now let us discuss with the help
3 min read
Hashtable in Java
Let us start with a simple Java code snippet that demonstrates how to create and use a HashTable. [GFGTABS] Java import java.util.Hashtable; public class HashtableCreation { public static void main(String args[]) { // Create a Hashtable of String keys and Integer values Hashtable<String, Integer> hashtable = new Hashtable<>(); // Adding
14 min read
Dictionary remove() Method in Java
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 va
2 min read
Java lang.Long.builtcount() method in Java with Examples
java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed Returns: the number of set bits in the binary repr
3 min read
Java.lang.Short toString() method in Java with Examples
toString(short) The static toString() method of java.lang.Short returns a new String object representing the specified short. The radix is assumed to be 10.This is a static method hence no object of Short class is required for calling this method. Syntax: public static String toString(short b) Parameters: This method accepts a parameter b which is
2 min read
Java.util.ArrayList.add() Method in Java
Below are the add() methods of ArrayList in Java: boolean add(Object o) : This method appends the specified element to the end of this list. Parameters: object o: The element to be appended to this list. Exception: NA // Java code to illustrate add(Object o) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static vo
2 min read
Lex program to take input from file and remove multiple spaces, lines and tabs
FLEX (Fast Lexical Analyzer Generator) is a tool/computer program for generating lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The function yylex() is the main flex function which
2 min read
Remove duplicates from unsorted array using Set data structure
Given an unsorted array of integers, print the array after removing the duplicate elements from it. We need to print distinct array elements according to their first occurrence. Examples: Input: arr[] = { 1, 2, 5, 1, 7, 2, 4, 2} Output: 1 2 5 7 4 Explanation: {1, 2} appear more than one time. Input: arr[] = { 3, 3, 4, 1, 1} Output: 3 4 1 Approach:
3 min read
Remove all negatives from the given Array
Given an array arr[] of size N, the task is to remove all negative elements from this array. Examples: Input: arr[] = {3, -4}Output: {3}Explanation: The only negative element of the array is -4. Input: arr[] = {1, -3, 2}Output: {1, 2} Approach 1: The given problem can be solved using the following steps : Create a vector newArr to store only positi
7 min read
Remove last character from the file in Python
Given a text file our task is to delete the last character from the file using Python Example: Input: File before removing the last character GeeksforGeeks Output: File after removing the last character GeeksforGeekExample 1: Using the seek and truncate function C/C++ Code with open('geeks.txt','w') as file_reader: file_reader.write("GeeksforG
2 min read
Remove all the occurrences of an element from a list in Python
The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Example Input1: 1 1 2 3 4 5 1 2 1 Output1: 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [2, 3, 4, 5, 2]Remove all Occurrences of an Item fr
4 min read
Python | C++ | Remove leading zeros from an IP address
Given an IP address, remove leading zeros from the IP address. Examples: Input : 100.020.003.400 Output : 100.20.3.400 Input :001.200.001.004 Output : 1.200.1.4Recommended PracticeRemove leading zeros from an IP addressTry It! The approach is to split the given string by “.” and then convert it to an integer which removes the leading zeros and then
4 min read
Article Tags :
Practice Tags :