TreeMap extends AbstractMap and implement NavigableMap
treeMap is a k-v pair set with order based on red-black tree’s navigableMap to implement. we can custom of sort principle or just use the default with based on the key.
The Defination of Red-black tree:(keep in mind, black nodes are way more than red)
- node is either red or black
- root is black
- every leaf is black
- every red node’s child node must be all black(means for every path, no two adjacent red node)
- every path has the exactly same number of black nodes.
In summary, red black tree is a balanced binary search tree with less balance than AVL tree(because in order to maintain a perfect balance tree like AVL, we have to cost lot when add and remove)
- the key of TreeMap can’t be null whereas hashmap can.
- treemap is not sync-ed
- treemap is slower than hashmap in search(), insert(), delete()
- TreeMap(Comparator<? super K> comparator)
- TreeMap(Map<? extends K,? extends V> m)
- TreeMap(SortedMap<K,? extends V> m)
the function in Hashmap is the same as Treemap, feel free to use them.
The relationship between Treeset and hashset is exactly the same with TreeMap and hashmap.