Unexpected complexity of common methods (size) in Java Collections Framework?

前端 未结 1 405
清歌不尽
清歌不尽 2021-02-13 09:41

Recently, I\'ve been surprised by the fact that some Java collections don\'t have constant time operation of method size().

While I learned that concurrent implementatio

相关标签:
1条回答
  • 2021-02-13 10:23

    For example, TreeSet.tailSet returns a view of the portion of backing set whose elements are greater than or equal to fromElement. What surprised me a lot is that calling size on returned SortedSet is linear in time, that is O(n).

    To me it is not surprising. Consider this sentence from the javadoc:

    "The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa."

    Since the tail set is a dynamic view of the backing set, it follows that its size has to be calculated dynamically in practice. The alternative would require that when a change was made to the backing set, it would have to adjust the sizes of all extant tailset (and headset) views. That would make updates to the backing set more expensive, AND it would present a storage management problem. (In order to update the view sizes, the backing set would need references to all existing view sets ... and that is a potential hidden memory leak.)

    Now you do have a point regarding the documentation. But in fact, the javadocs says nothing about the complexity of the view collections. And, indeed, it doesn't even document that TreeSet.size() is O(1)! In fact, it only documents the complexity of the add, remove and contains operations.


    I would like to know is there a detailed and complete documentation on performance of many such operations especially ones which are completely unexpected?

    AFAIK, No. Certainly, not from Sun / Oracle ...

    0 讨论(0)
提交回复
热议问题