Monday, November 7, 2011

HashMap and Hashtable

HashMap:

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings).

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor.
The capacity     -  number of buckets in the hash table,
Initial capacity  - the capacity at the time the hash table is created.
The load factor - measure of how full the hash table is allowed to get before its capacity is automatically increased.

When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets

HashTable:
Wont permit null keys and null values.
Synchronised(thread safe).

Concurrent HashMap:
Like Hashtable but unlike HashMap, this class does not allow null to be used as a key or value.
If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

No comments:

Post a Comment