Home Navigation

Friday 17 March 2017

Java collections comparison


HashMap
TreeMap
Ordering
It doesn’t maintain ordering
TreeMap  elements are sorted according to the natural ordering of its elements . If TreeMap objects can not be sorted in natural order than use compareTo() method to sort the elements of TreeMap object.
Implementation
Hashing
Red black tree
Null key
Can store null key
No null key
Null values
Many null values
Many null values
Performance
Constant time O(1) get and put, fast in performance.
TreeMap provides guaranteed log(n) time cost for the get and put method. Slow in comparison
Comparison
Uses equals method
Uses compareTo method
Interface
Map
Navigational Map
Similarities
Not thread safe, shallow copy, fail fast iterator

HashTable
HashMap
LinkedHashMap
Synchronized
Unsynchronized
It is a subclass of HashMap. It inherits all the features of HashMap. In addition linkedlist preserved the insertion order.
Doesn’t permit null key
Permits one null key

No comments:

Post a Comment