This example shows how to check if the key exists in the Hashtable in Java. This example also shows how to check if the key exists in Hashtable using the containsKey and get methods.
How to check if the key exists in the Hashtable in Java?
There are a couple of ways to check if the specified key exists in the hashtable object.
1. Using the get method
The Hashtable get method returns the value mapped to the specified key in the hashtable object.
1 |
public V get(Object key) |
It returns null if the key is not mapped to any value in the hashtable object. We can compare the return value of the get
method with null to check if the hashtable contains the key as given below.
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 |
import java.util.Hashtable; public class HashtableCheckIfKeyExists { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * If the return value of the get * method is null, then the key * does not exist */ //this will print true as the key 2 exists System.out.println( "Key 2 exists? " + (hashtable.get(2) != null) ); /* * this will print false as the key 5 * does not exist and hence the get * method will return null */ System.out.println( "Key 5 exists? " + (hashtable.get(5) != null) ); } } |
Output
1 2 |
Key 2 exists? true Key 5 exists? false |
2. Using the containsKey method
The Hashtable containsKey
method returns true if the specified key exists in the hashtable object.
1 |
public boolean containsKey(Object key) |
It returns false if the specified key is not mapped to any value in the hash table object.
1 2 3 4 5 6 7 8 9 |
hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //this will print true as the key 2 exists System.out.println( "Key 2 exists? " + hashtable.containsKey(2) ); //this will print false as the key 5 does not exist System.out.println( "Key 5 exists? " + hashtable.containsKey(5) ); |
Output
1 2 |
Key 2 exists? true Key 5 exists? false |
How to check if the custom class key exists in the Hashtable in Java?
Let’s see what happens when the hashtable keys are objects 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 |
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 HashtableCheckIfKeyExists { public static void main(String[] args) { Hashtable<User, Integer> hashtable = new Hashtable<User, Integer>(); hashtable.put(new User(1), 1); hashtable.put(new User(2), 2); System.out.println( hashtable.containsKey(new User(2) ) ); } } |
Output
1 |
false |
As we can see from the output, even if the key was present in the hashtable, the containsKey
method returned false.
The containsKey
method relies on the equals
method to compare the key objects. If the hashtable contains keys of a custom class, then the custom class must implement the equals
and hashCode
methods.
Let’s override these methods in the User custom 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 |
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 HashtableCheckIfKeyExists { public static void main(String[] args) { Hashtable<User, Integer> hashtable = new Hashtable<User, Integer>(); hashtable.put(new User(1), 1); hashtable.put(new User(2), 2); System.out.println( hashtable.containsKey(new User(2) ) ); } } |
Output
1 |
true |
Which method should I use (get vs containsKey method)?
The containsKey
method makes the code more readable and cleaner as compared to comparing the return value of the get
method with the null.
However, if you are going to get the value for the key after checking if it exists, using the get
method is preferred to save an extra call. In all other scenarios, using the containsKey
method is preferred.
This example is a part of the Hashtable in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation