Computational complexity of TreeSet operations in Java?

后端 未结 4 1457
暗喜
暗喜 2021-01-04 17:43

I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says:

\"This implementation provides

4条回答
  •  甜味超标
    2021-01-04 18:15

    Looking at the java source for TreeSet, it looks like it if the passed in collection is a SortedSet then it uses a O(n) time algorithm. Otherwise it calls super.addAll, which I'm guessing will result in O(n logn).

    EDIT - guess I read the code too fast, TreeSet can only use the O(n) algorithm if it's backing map is empty

提交回复
热议问题