SortedDictionary和SortedList

时间:2022-06-15 23:08:56

使用上两者的接口都类似字典,并且SortedList的比如Find,FindIndex,RemoveAll常用方法都没提供。

数据结构上二者差异比较大,SortedList查找数据极快,但添加新元素,删除元素较慢,SortedDictionary查找,添加,删除速度都比较平均。

博友的测试结果:

SortedDictionary和SortedList

直接在Unity3D里测一下

public class Test : MonoBehaviour
{
void Start()
{
var sortedDict = new SortedDictionary<string, int>();
sortedDict.Add("a01", );
sortedDict.Add("a10", );
sortedDict.Add("a03", );
sortedDict.Add("a02", ); print(sortedDict["a01"]); foreach (var item in sortedDict)
{
Debug.Log("SortedDictionary: " + item);
} var sortedList = new SortedList<string, int>();
sortedList.Add("a01", );
sortedList.Add("a10", );
sortedList.Add("a03", );
sortedList.Add("a02", ); print(sortedList["a01"]); foreach (var item in sortedList)
{
Debug.Log("SortedList: " + item);
}
}
}

调用结果:

SortedDictionary和SortedList