Hashset vs Treeset

后端 未结 14 1905
再見小時候
再見小時候 2020-11-22 13:38

I\'ve always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I\'ve ever known has asked me pointedly why I would u

14条回答
  •  渐次进展
    2020-11-22 14:14

    One advantage not yet mentioned of a TreeSet is that its has greater "locality", which is shorthand for saying (1) if two entries are nearby in the order, a TreeSet places them near each other in the data structure, and hence in memory; and (2) this placement takes advantage of the principle of locality, which says that similar data is often accessed by an application with similar frequency.

    This is in contrast to a HashSet, which spreads the entries all over memory, no matter what their keys are.

    When the latency cost of reading from a hard drive is thousands of times the cost of reading from cache or RAM, and when the data really is accessed with locality, the TreeSet can be a much better choice.

提交回复
热议问题