Monday, 26 October 2015

Difference between HashMap, LinkedHashMap and TreeMap?


Property

HashMap

TreeMap

LinkedHashMap


Order

No guarantee order will remain same over time.

Sorted according to natural ordering or the accoding to comparator passed at the time of creation.


Insertion order.

Get/put/ remove/
containsKey
    

O(1)

O(log(n))

O(1)

Interfaces

Map

Map, SortedMap NavigableMap.


Map

Null values/keys

Allowed

Only null values.
NullPointerExceptionin case of null keys.


Allowed

Fail-fast behaviour

All three are fail fast i.e. throws ConcurrentModificationException.


Implementation

Buckets

Red-Black Tree

Double-linked buckets

Is synchronized

Implementation is not synchronized.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...