C# - SortedList<TKey, TValue>
SortedList<TKey, TValue> 和 SortedList 是可以存储键值对的集合类,这些键值对基于关联的 IComparer 实现按键排序。 例如,如果键是原始类型,则按键Key的升序排序。
C# 支持泛型和非泛型 SortedList。 建议使用通用 SortedList<TKey, TValue>,因为它比非通用 SortedList 执行速度更快且不易出错。
SortedList 特征
- SortedList<TKey, TValue> 是按键排序的键值对数组。
- 添加元素后立即对其进行排序。 根据 IComparer<T> 按升序对原始类型键和对象键进行排序。
- 属于 System.Collection.Generic 命名空间。
- 键必须是唯一的,不能为空。
- 值可以为空或重复。
- 可以通过在索引器 mySortedList[key] 中传递关联的键来访问值
- 包含 KeyValuePair<TKey, TValue> 类型的元素
- 它比 SortedDictionary<TKey,TValue> 使用更少的内存。
- 排序后的数据检索速度更快,而 SortedDictionary<TKey, TValue> 在插入和删除键值对方面更快。
创建SortedList
以下示例演示如何创建泛型 SortedList<TKey, TValue>,并在其中添加键值对。
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>();
sortedList.Add(22, "abc22");
sortedList.Add(11, "qqq11");
sortedList.Add(2, "abc2");
sortedList.Add(1, "qqq1");
sortedList.Add(13, "sdfsdf13");
Console.WriteLine(string.Join(",", sortedList.Select(it => it.Value)));
//qqq1,abc2,qqq11,sdfsdf13,abc22
}使用 集合初始值设定项 语法 来初始化一个SortedList,如下。
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
{22, "abc22" },
{23, "abc23" },
{3, "abc3" },
};
Console.WriteLine(string.Join(",", sortedList.Select(it => it.Value)));
//abc3,abc22,abc23
}C#6.0 之后我们可以 使用 索引初始化Index Initializers 索引初始化器 语法 来初始化一个SortedList,如下。
这种看起来会更清晰
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
[22] = "abc22",
[23] = "abc23",
[3] = "abc3",
};
Console.WriteLine(string.Join(",", sortedList.Select(it => it.Value)));
//abc3,abc22,abc23
}使用foreach循环 列出所有的key和value
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
[22] = "abc22",
[23] = "abc23",
[3] = "abc3",
};
foreach (var item in sortedList) // item 是 KeyValuePair<int, string> 类型
{
Console.WriteLine($"{item.Key}={item.Value}");
}
}输出
shell
3=abc3
22=abc22
23=abc23访问SortedList
在索引器 sortedList[key] 中指定一个键,以获取或设置 SortedList 中的值。
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
[22] = "abc22",
[23] = "abc23",
[3] = "abc3",
};
Console.WriteLine(sortedList[22]);//"abc22"
sortedList[3] = "abc3 new"; //更改了值
sortedList[33] = "abc33"; // 添加了一个新元素
//Console.WriteLine(sortedList[65]); //System.Collections.Generic.KeyNotFoundException:“The given key '65' was not present in the dictionary.”
}上面, sortedList[65] 将抛出 KeyNotFoundException 因为指定的键 65 在排序列表中不存在。 为防止出现此异常,请使用 ContainsKey() 或 TryGetValue() 方法,如下所示。
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
[22] = "abc22",
[23] = "abc23",
[3] = "abc3",
};
if (sortedList.ContainsKey(65))
{
Console.WriteLine(sortedList[65]); //不会执行到
}
if (sortedList.TryGetValue(3, out string value))
{
Console.WriteLine(value); //abc3
}
}sortedList还支持Keys和Values。
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
[22] = "abc22",
[23] = "abc23",
[3] = "abc3",
};
foreach (var item in sortedList.Keys) //.Values也是可以输出所有的Values
{
Console.Write(item + ",");
}
//3,22,23,
}从SortedList当中移除元素
使用 Remove(key) 和 RemoveAt(index) 方法从 SortedList 中删除键值对。
csharp
static void Main(string[] args)
{
var sortedList = new SortedList<int, string>()
{
[22] = "abc22",
[23] = "abc23",
[3] = "abc3",
[4] = "abc4",
[5] = "abc5",
[6] = "abc5",
};
sortedList.RemoveAt(2); //移掉了 [5] = "abc5",
sortedList.Remove(23); //移掉了 [23] = "abc23",
Console.WriteLine(string.Join(",", sortedList.Keys)); //3,4,6,22
}查看 源码
