Java provides two data structures for hashtables: one is Hashtable and HashMap.

HashTable HashMap
synchronized and performs poorly in a single threaded environment unsynchronized
not allow null key or null value one null key and any number of null values

HashMap is generally preferred, unless you have to deal with threads and synchronization. Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.

Synchronized means only a single thread can modify a hashtable at one point of time. Basically, that means any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

HashMap uses "bucket" to store key-value pairs. Each bucket has a unique number.

Add: (key, value) -> calculate the hash code and find the bucket. If a collision happens, hashmap will use linked list to store object. It is important to note that, one bucket can store more then one key-value pair.

search: Use the hash code of the key. If more than one object in the bucket, do the linear search using equals() method.

results matching ""

    No results matching ""