This example shows how to remove key-value mapping or entry from the Hashtable in Java. This example also shows how to remove hashtable entry using the remove method.
How to remove a key-value mapping or entry from the Hashtable in Java?
The Hashtable remove
method removes the specified key and the value associated with it from the hashtable object.
1 |
public V remove(Object key) |
It returns the value mapped to the given key if the specified key is found and removed from the hashtable, null otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * Use the remove method to remove key-value * from the hashtable */ /* * this will remove 2->Two entry from the hashtable * and return "Two" */ System.out.println( hashtable.remove(2) ); //this will return null as the key 5 does not exist System.out.println( hashtable.remove(5) ); System.out.println("Hashtable contains: " + hashtable); |
Output
1 2 3 |
Two null Hashtable contains: {3=Three, 1=One} |
The above given remove
method removes the key-value from the hashtable if the key is mapped to any value in the hashtable. If you want to remove the entry only if the key is mapped to a specific value, you can use the below given overloaded remove
method.
1 |
public boolean remove(Object key, Object value) |
It removes the entry if and only if the given key is mapped to the given value in the hashtable and returns true. If the given key does not exist or the key is not mapped to the given value, it returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * this will remove 2->Two entry from the hashtable * and return true because key 2 exists and it is * mapped to value "Two" */ System.out.println( hashtable.remove(2, "Two") ); /* * this will return false as the key 5 * does not exist */ System.out.println( hashtable.remove(5, "One") ); /* * this will also return false as the key 3 * exists but it is not mapped to value "Four" */ System.out.println( hashtable.remove(3, "Four") ); System.out.println("Hashtable contains: " + hashtable); |
Output
1 2 3 4 |
true false false Hashtable contains: {3=Three, 1=One} |
How to remove key-value mappings from the Hashtable of custom class objects?
Let’s try to remove an entry from the hashtable having keys of a custom class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Hashtable; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } } public class HashtableRemoveExample { public static void main(String[] args) { Hashtable<User, Integer> htUsers = new Hashtable<User, Integer>(); htUsers.put(new User(101), 101); htUsers.put(new User(102), 102); System.out.println( htUsers.remove(new User(101)) ); System.out.println( htUsers ); } } |
Output
1 2 |
null {User -> 101=101, User -> 102=102} |
As we can see from the output, even if the hashtable contains the key, the remove
method returns null and does not remove the entry from the hashtable.
The remove
method relies on the equals
and hashCode
methods to compare and find the object to be removed. Since the User class has not overridden these methods, methods inherited from the Object class were called which compare the object references, not the actual content. That was the reason the remove
method could not find the key in the hashtable object.
Let’s override the equals
and hashCode
methods in the User class and try again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import java.util.Hashtable; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id != other.id) return false; return true; } } public class HashtableRemoveExample { public static void main(String[] args) { Hashtable<User, Integer> htUsers = new Hashtable<User, Integer>(); htUsers.put(new User(101), 101); htUsers.put(new User(102), 102); System.out.println( htUsers.remove(new User(101)) ); System.out.println( htUsers ); } } |
Output
1 2 |
101 {User -> 102=102} |
Tip: Always override the equals
and hashCode
methods in the custom classes while working with any Java Collection classes.
This example is a part of the Java Hashtable Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation