@DanielDong said in Java TreeMap:

What is the time complexity of this solution?

TreeMap internally uses Red-Black trees, and the operations for containsKey, get, put, remove takes O(log n).

So in this case if n is number of ranges at any point in time, even if it is O(log n) for add/remove, cleaning up takes O(n) in the worst case.

For queryRange, it is gauranteed O(log n)

Source : https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html#put(K, V)