Wednesday, November 17, 2010

HashMap Implementation notes

Implements Map interface

Permits null values and null key

None of the methods of HashMap are synchronized, so if you are adding or deleting keys - you must synchronize in multi threaded environment. Synchronize on some object, if  external object is not available use Collections.synchronizedMap(new HashMap())

Collections.synchronizedMap(new HashMap()) synchronizes all map operations using a internal mutex. However synchronization every single method is not enough, some client side locking is also required.

Iterators of HashMap are fail-fast when underlying map is modified (add/delete key) - ConcurrentModificationException is thrown.

Ordering of keys is not guaranteed to be same over time.

Access for get and put is time constant.

Max Capacity ~ 1.07 billiion

Default Initial capacity = 16

Uses hash function to disperse keys around.--

Uses Array to store elements.

Try and provide capacity  as part of initialization.