This example shows how to check if a value exists in Hashtable in Java. This example also shows how to check if the value exists in the Hashtable using the contains and containsValue methods.
How to check if a value exists in Hashtable in Java?
There are a couple of ways using which we can check if the hashtable contains the specified value mapped to any key.
1. Using the contains method
The Hashtable contains
method returns true if the specified value exists in the hashtable object.
1 |
public boolean contains(Object value) |
It returns true if the specified value is mapped to one or more keys in the hashtable object. It returns false if the specified value is not mapped to any key in the hash table object.
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; public class HashtableContainsValue { 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"); /* * To check if the value is mapped to * any keys in the Hashtable object, use * the contains method */ /* * this will return true as the value "Two" * is mapped to key 2 */ System.out.println( hashtable.contains("Two") ); /* * this will return false as the value "Five" * is not mapped to any key in the hash table */ System.out.println( hashtable.contains("Five") ); } } |
Output
1 2 |
true false |
2. Using the containsValue method
The Hashtable containsValue
has the same functionality as the contains
method.
1 |
public boolean containsValue(Object value) |
it returns true if the specified value is mapped to one or more keys in the hash table, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //this will return true System.out.println( hashtable.containsValue("Two") ); //this will return false System.out.println( hashtable.containsValue("Five") ); |
Output
1 2 |
true false |
Which method should I use to check (contains vs containsValue method)?
The contains
and containsValue
method are similar in functionality. In fact, if you look at the source code of the Hashtable class, the containsValue
method simply calls the contains
method.
Then why there are two methods? Well, in Java 2 the Hashtable class was rewritten to implement the Map interface. The class implementing an interface must define all the methods declared in the interface so the Hashtable class had to define the containsValue
method declared in the Map interface.
How to check if a custom class value exists in Hashtable in Java?
Let’s first see what happens when the hash table values 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 HashtableContainsValue { public static void main(String[] args) { Hashtable<Integer, User> htUsers = new Hashtable<Integer, User>(); htUsers.put(1, new User(101)); htUsers.put(2, new User(102)); System.out.println( htUsers.containsValue(new User(102)) ); } } |
Output
1 |
false |
As we can see from the output, the containsValue
method returned false even though the hash table has the specified object. The reason is, the contains
and containsValue
methods rely on the equals
and hashCode
methods to compare the objects.
Let’s override these 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 |
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 HashtableContainsValue { public static void main(String[] args) { Hashtable<Integer, User> htUsers = new Hashtable<Integer, User>(); htUsers.put(1, new User(101)); htUsers.put(2, new User(102)); System.out.println( htUsers.containsValue(new User(102)) ); } } |
Output
1 |
true |
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