hashtable example in java: Want to learn about the Java Hashtable? Then, this is the best & ultimate tutorial for you all. Hashtable stores the key/values pair in a hash table. Both HashMap and HashTable are similar but there are few differences between them which can be known clearly from here.
Also, we will see how to create a Hashtable, how it works internally, methods of java hashtable, some example codes, etc. So, stay tuned to this page and gather enough information on Hashtable Class in Java.
- Hashtable in Java
- Java Hashtable class declaration
- Parameters of HashTable Class
- The Hierarchy of Hashtable Class in Java
- How Hashtable works internally in java?
- Features about Java Hashtable
- Constructors of Java Hashtable Class
- Methods of Hashtable Class in Java
- Java Hashtable Example
- Hashtable vs HashMap
- Comparison Table of HashMap & HashTable
Hashtable in Java
Java Hashtable Class inherits Dictionary class and implements the Map interface. Also, This class implements a hash table. Moreover, it was a part of java.util package. Java Hashtable is the same as HashMap besides it is synchronized.
Java Hashtable class declaration
Here is the declaration for java.util.Hashtable class:
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Parameters of HashTable Class
- K: It is the type of keys maintained by this map.
- V: It is the type of mapped values.
The Hierarchy of Hashtable Class in Java
Do Read:
How Hashtable works internally in java?
A Hashtable internally holds buckets in which it stores the key/value pairs. The Hashtable uses the key’s hashcode to specify which bucket the key/value pair should map. When you give a key/value to the Hashtable, it asks the key’s hashcode. The Hashtable uses that code to determine the bucket in which to place the key/value.
Thus, for instance, if the hashcode equals zero, the Hashtable stores the value into Bucket 0. Furthermore, if the hashcode is two, the Hashtable stores the value into Bucket 2. (This is a simple example; the Hashtable will massage the hashcode first so the Hashtable doesn’t try to insert the value outside the bucket.)
With the help of this hashcode approach, the Hashtable can also instantly determine in which bucket it has stored the value when you decide to retrieve it. The hashcode only says the Hashtable into which bucket to drop the key/value. Sometimes, still, multiple objects may map to the same bucket, an event called a collision. In Java, the Hashtable reacts to a collision by storing multiple values into the same bucket (other implementations may work collisions differently).
Features about Java Hashtable
- Java Hashtable is the best option in case of frequent operation is to store and retrieves objects from a hash table.
- Java Hashtable uses a hash table data structure.
- It implements a Serializable, Cloneable interface but not RandomAccessInterface.
- Hashtable is thread-safe because every method determines in the Java Hashtable class is synchronized.
- Insertion order is not stored and it depends on the hash code of keys.
- Duplicates keys are not accepted but values may be duplicated.
- Heterogeneous objects are allowed.
- Java Hashtable class includes unique elements.
- Java Hashtable doesn’t support null keys or null values.
Constructors of Java Hashtable Class
Java Hashtable represents the following constructors.
- Hashtable(): The first constructor is used to create a default hash table.
- Hashtable(int size): This constructor is used to creates a hash table that has an initial size specified by size.
- Hashtable(int size, float load factor): This constructor is used to creates a hash table that has an initial size specified by size and a load factor is specified by the load factor.
- Hashtable(Map m): This constructor is used to creates a hash table that is initialized with the elements in m.
Methods of Hashtable Class in Java
- void clear(): This method is used to removes all key/values pair from a hash table and make it empty.
- Object clone(): It creates a shallow copy of this hash table.
- boolean contains(Object value): This method returns true if some value equal to value exists within the hash table and returns false if the key is not found.
- boolean containsKey(Object key): This method returns true if some key equal to key exists within the hash table else it returns false.
- boolean containsValue(Object value): It returns true if some value equal to value exists within the hash table else it returns false.
- Enumeration elements(): It returns an enumeration of the values contained in the hash table.
- Object get(Object key): This method is used to remove the object that contains the value associated with a key and returned a null object if the key is not in the hash table.
- boolean isEmpty(): It returns true if the hash table is empty else returns false.
- Enumeration keys(): It returns an enumeration of the keys contained in the hash table.
- Object put(Object key, Object value): This method is used to insert a key-value pair in the hash table.
- void rehash(): This method is used to increases the size of the hash table and rehashes all of its keys.
- Object remove(Object key): It removes a key and its value and returned a null object if the key is not in the hash table.
- int size(): It returns the number of entries in the hash table.
- String toString(): This method is used to returns the string equivalent of a hash table.
Java Hashtable Example
import java.util.*; class HashtableExample{ public static void main(String args[]){ //creating a hash table Hashtable<String, String> ht = new Hashtable<String, String>(); Enumeration names; String key; //adding values in this hash table ht.put("Key 1", "Rahul"); ht.put("Key 2", "Amit"); ht.put("Key 3", "Vijay"); ht.put("Key 4", "Suresh"); ht.put("Key 5", "Prashant"); //returns size of the hash table System.out.println("Hash table size is: "+ht.size()); //checks hash table is empty or not System.out.println("Hash table is empty or not: "+ht.isEmpty()); //returns true if hash table contains specified value System.out.println("Hash table contains specified value or not: "+ht.containsValue("Prashant")); names = ht.keys(); while(names.hasMoreElements()){ key = (String)names.nextElement(); System.out.println("Key is: " +key + " & its Value is: " +ht.get(key)); } ht.clear(); System.out.println("Removes all the key-value pair: "+ht); } }
Output:
Hashtable vs HashMap
Hashtable and HashMap both are part of the java.util and are utilized to save data in the form of key-value pairs. Hashing techniques are used by both to store unique keys. However, there are few differences between Hashtable and HashMap which is very essential to know for achieving the Java related interviews. Let’s focus on the differences between Hashtable and HashMap by viewing the following points:
- Hashtable is synchronized while the HashMap is not synchronized.
- Hashtable doesn’t allow you to store null key and null value on the other hand HashMap allows you to store one null key and multiple null values.
- HashMap was introduced in Java 1.2 while Hashtable is a legacy class.
- HashMap is fast while Hashtable is slow due to synchronization.
- HashMap is traversed by Iterator while Hashtable is traversed by both Enumeration and Iterator.
- HashMap inherits the AbstractMap class on the other hand Hashtable inherits the dictionary class.