Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeMap - Search Time Complexity

What is the time complexity of a get() and put() in a TreeMap?

Is the implementation same as a Red-Black Tree?

like image 961
java_geek Avatar asked May 19 '10 09:05

java_geek


People also ask

Is TreeMap slower than HashMap?

HashMap provides expected constant-time performance O(1) for most operations like add(), remove() and contains(). Therefore, it's significantly faster than a TreeMap.

Does TreeMap use binary search?

In Java, a TreeMap is a red-black tree, which is a self-balancing binary search tree.

What is the time complexity of basic operations get () and put () in HashMap class?

Hashmap put and get operation time complexity is O(1) with assumption that key-value pairs are well distributed across the buckets.


1 Answers

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

like image 143
Daniel Renshaw Avatar answered Oct 14 '22 16:10

Daniel Renshaw