猿问

SortedList和SortedDictionary有什么区别?

a SortedList<TKey,TValue>和a 之间是否有任何实际的区别SortedDictionary<TKey,TValue>?在任何情况下你会专门使用一个而不是另一个吗?

烙印99
浏览 985回答 3
3回答

三国纷争

如果它有帮助,这是一个表格视图...从绩效角度来看:+------------------+---------+----------+--------+----------+----------+---------+| Collection&nbsp; &nbsp; &nbsp; &nbsp;| Indexed | Keyed&nbsp; &nbsp; | Value&nbsp; | Addition |&nbsp; Removal | Memory&nbsp; ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | lookup&nbsp; | lookup&nbsp; &nbsp;| lookup |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|+------------------+---------+----------+--------+----------+----------+---------+| SortedList&nbsp; &nbsp; &nbsp; &nbsp;| O(1)&nbsp; &nbsp; | O(log n) | O(n)&nbsp; &nbsp;| O(n)*&nbsp; &nbsp; | O(n)&nbsp; &nbsp; &nbsp;| Lesser&nbsp; || SortedDictionary | n/a&nbsp; &nbsp; &nbsp;| O(log n) | O(n)&nbsp; &nbsp;| O(log n) | O(log n) | Greater |+------------------+---------+----------+--------+----------+----------+---------+* Insertion is O(1) for data that are already in sort order, so that each&nbsp;&nbsp; element is added to the end of the list (assuming no resize is required).从实施角度来看:+------------+---------------+----------+------------+------------+------------------+| Underlying | Lookup&nbsp; &nbsp; &nbsp; &nbsp; | Ordering | Contiguous | Data&nbsp; &nbsp; &nbsp; &nbsp;| Exposes Key &&nbsp; &nbsp; || structure&nbsp; | strategy&nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | storage&nbsp; &nbsp; | access&nbsp; &nbsp; &nbsp;| Value collection |+------------+---------------+----------+------------+------------+------------------+| 2 arrays&nbsp; &nbsp;| Binary search | Sorted&nbsp; &nbsp;| Yes&nbsp; &nbsp; &nbsp; &nbsp; | Key, Index | Yes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || BST&nbsp; &nbsp; &nbsp; &nbsp; | Binary search | Sorted&nbsp; &nbsp;| No&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Key&nbsp; &nbsp; &nbsp; &nbsp; | Yes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |+------------+---------------+----------+------------+------------+------------------+要大致套用,如果您需要原始性能SortedDictionary可能是一个更好的选择。如果您需要较少的内存开销,索引检索SortedList更适合。有关何时使用哪个,请参阅此问题。
随时随地看视频慕课网APP
我要回答