What is the time complexity of a get() and put() in a TreeMap?
Is the implementation same as a Red-Black Tree?
HashMap provides expected constant-time performance O(1) for most operations like add(), remove() and contains(). Therefore, it's significantly faster than a TreeMap.
In Java, a TreeMap is a red-black tree, which is a self-balancing binary search tree.
Hashmap put and get operation time complexity is O(1) with assumption that key-value pairs are well distributed across the buckets.
From here: http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With