Check if a key exists in Hashtable in Java
The Method
containsKey() returns true if the object you are looking for is in the Hashtable list, otherwise it returns false. The search is done with the key.
import java.util.Hashtable;
public class main {
public static void main(String[] s) {
Hashtable table = new Hashtable();
table.put("key1", "v1");
table.put("key2", "v2");
table.put("key3", "v3");
if(table.containsKey("key2"))
System.out.println("key2 exists in Hashtable");
else
System.out.println(table.containsKey("key2 does not exist in Hashtable");
}
}
Runtime:
key2 exists in Hashtable