在SortedList<TKey,TValue>和SortedDictionary<TKey,TValue>之间有任何真正的实际区别吗?在某些情况下,你会特别使用其中一种而不是另一种吗?


当前回答

是的,它们的表现特征有很大不同。将它们命名为SortedList和SortedTree可能更好,因为这样可以更准确地反映实现。

查看它们各自的MSDN文档(SortedList, SortedDictionary),了解不同情况下不同操作的性能细节。下面是一个很好的总结(来自SortedDictionary文档):

The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedList<TKey, TValue> generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal: SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>. SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>. If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

(SortedList实际上维护一个排序的数组,而不是使用树。它仍然使用二进制搜索来查找元素。)

其他回答

查看SortedList的MSDN页面:

备注部分:

The SortedList<(Of <(TKey, TValue>)>) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary<(Of <(TKey, TValue>)>) generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal: SortedList<(Of <(TKey, TValue>)>) uses less memory than SortedDictionary<(Of <(TKey, TValue>)>). SortedDictionary<(Of <(TKey, TValue>)>) has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<(Of <(TKey, TValue>)>). If the list is populated all at once from sorted data, SortedList<(Of <(TKey, TValue>)>) is faster than SortedDictionary<(Of <(TKey, TValue>)>).

我打开Reflector来看看这个,因为似乎有一些关于SortedList的困惑。它实际上不是一个二叉搜索树,它是一个由键-值对排序的数组。还有一个TKey[]键变量,它与键值对同步排序,用于二进制搜索。

这里有一些源代码(针对。net 4.5)来备份我的声明。

私有成员

// Fields
private const int _defaultCapacity = 4;
private int _size;
[NonSerialized]
private object _syncRoot;
private IComparer<TKey> comparer;
private static TKey[] emptyKeys;
private static TValue[] emptyValues;
private KeyList<TKey, TValue> keyList;
private TKey[] keys;
private const int MaxArrayLength = 0x7fefffff;
private ValueList<TKey, TValue> valueList;
private TValue[] values;
private int version;

SortedList。ctor (IDictionary IComparer)

public SortedList(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer) : this((dictionary != null) ? dictionary.Count : 0, comparer)
{
    if (dictionary == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dictionary);
    }
    dictionary.Keys.CopyTo(this.keys, 0);
    dictionary.Values.CopyTo(this.values, 0);
    Array.Sort<TKey, TValue>(this.keys, this.values, comparer);
    this._size = dictionary.Count;
}

SortedList。Add(TKey, TValue): void

public void Add(TKey key, TValue value)
{
    if (key == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
    }
    int num = Array.BinarySearch<TKey>(this.keys, 0, this._size, key, this.comparer);
    if (num >= 0)
    {
        ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
    }
    this.Insert(~num, key, value);
}

SortedList.RemoveAt(int): void

public void RemoveAt(int index)
{
    if ((index < 0) || (index >= this._size))
    {
        ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
    }
    this._size--;
    if (index < this._size)
    {
        Array.Copy(this.keys, index + 1, this.keys, index, this._size - index);
        Array.Copy(this.values, index + 1, this.values, index, this._size - index);
    }
    this.keys[this._size] = default(TKey);
    this.values[this._size] = default(TValue);
    this.version++;
}

是的,它们的表现特征有很大不同。将它们命名为SortedList和SortedTree可能更好,因为这样可以更准确地反映实现。

查看它们各自的MSDN文档(SortedList, SortedDictionary),了解不同情况下不同操作的性能细节。下面是一个很好的总结(来自SortedDictionary文档):

The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedList<TKey, TValue> generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal: SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>. SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>. If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

(SortedList实际上维护一个排序的数组,而不是使用树。它仍然使用二进制搜索来查找元素。)

索引访问(这里提到)是实际的区别。 如果需要访问后继对象或前任对象,则需要SortedList。SortedDictionary无法做到这一点,因此您在如何使用排序(first / foreach)方面受到相当的限制。

这是性能之间相互比较的可视化表示。