Javatpoint Logo

91-9990449935

 0120-4256464

Javatpoint Logo

Java Hashtable class

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

ConstructorDescription
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

MethodDescription
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.

Java Hashtable Example

Test it Now

Output:

103 Rahul
102 Ravi
101 Vijay
100 Amit
       

Java Hashtable Example: remove()

Output:

Values before remove: {103=Operating System, 102=Let us C, 101=Data Communication and Networking}
Values after remove: {103=Operating System, 101=Data Communication and Networking}

Java Hashtable Example: Book

Output:

3 Details:
103 Operating System Galvin Wiley 6
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
1 Details:
101 Let us C Yashwant Kanetkar BPB 8




Share this page

facebook twitter google plus pinterest tumbler

Latest 4 Tutorials