java tutorial - Java TreeMap class Java Hashtable class - java programming - learn java - java basics - java for beginners
Learn Java - Java tutorial - Java hash table - Java examples - Java programs
Java Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface.
The important points about Java Hashtable class are:
- A Hashtable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
- It contains only unique elements.
- It may have not have any null key or value.
- It is synchronized.
Hashtable class declaration
Let's see the declaration for java.util.Hashtable class.
Hashtable class Parameters
Let's see the Parameters for java.util.Hashtable class.
- K: It is the type of keys maintained by this map.
- V: It is the type of mapped values.
Constructors of Java Hashtable class
Constructor | Description |
---|---|
Hashtable() | It is the default constructor of hash table it instantiates the Hashtable class. |
Hashtable(int size) | It is used to accept an integer parameter and creates a hash table that has an initial size specified by integer value size. |
Hashtable(int size, float fillRatio) | It is used to create a hash table that has an initial size specified by size and a fill ratio specified by fillRatio. |
Methods of Java Hashtable class
Method | Description |
---|---|
void clear() | It is used to reset the hash table. |
boolean contains(Object value) | This method return true if some value equal to the value exist within the hash table, else return false. |
boolean containsValue(Object value) | This method return true if some value equal to the value exists within the hash table, else return false. |
boolean containsKey(Object key) | This method return true if some key equal to the key exists within the hash table, else return false. |
boolean isEmpty() | This method return true if the hash table is empty; returns false if it contains at least one key. |
void rehash() | It is used to increase the size of the hash table and rehashes all of its keys. |
Object get(Object key) | This method return the object that contains the value associated with the key. |
Object remove(Object key) | It is used to remove the key and its value. This method return the value associated with the key. |
int size() | This method return the number of entries in the hash table. |