C#冷门系列之SortedDictionary、SortedList

一.介绍:

SortedDictionary<key,value>,从字面意思不难看出来是有序字典

SortedList<key,value>,有序列表

二.使用:

            //SortedDictionary
            SortedDictionary<int, string> sortDic = new SortedDictionary<int, string>();
            sortDic[2] = "2";
            sortDic[1] = "1";
            sortDic[9] = "9";
            sortDic[4] = "4";
            Console.WriteLine("SortedDictionary:");
            foreach (var item in sortDic)
            {
                Console.WriteLine(item.Key);
            }

            //Dictionary
            Dictionary<int, string> Dic = new Dictionary<int, string>();
            Dic[2] = "2";
            Dic[1] = "1";
            Dic[9] = "9";
            Dic[4] = "4";
            Console.WriteLine("Dictionary:");
            foreach (var item in Dic)
            {
                Console.WriteLine(item.Key);
            }

            //SortedList
            SortedList<int, string> sortList = new SortedList<int, string>();
            sortList[2] = "2";
            sortList[1] = "1";
            sortList[9] = "9";
            sortList[4] = "4";
            Console.WriteLine("sortList:");
            foreach (var item in sortList)
            {
                Console.WriteLine(item.Key);
            }

控制台输出=>可以看出普通Dictionary,没有默认排序,SortedDictionary和SortedList则默认实现字典排序

 

三.SortedDictionary<Key,Value>、SortedList<Key,Value>的差异:

  1. SortedDictionary<Key,Value>使用的内存比SortedList<Key,Value>多
  2. 2.SortedDictionary<Key,Value>删除比较快,插入有序数据时SortedList<Key,Value>较快,而插入无序数据时SortedList<Key,Value>非常慢,查询SortedList<Key,Value>比SortedDictionary<Key,Value>较快。(亲测,环境net4.0),SortedList移除特别慢,耗时约为SortedDictionary的80倍。参考另一篇文章https://mp.csdn.net/console/editor/html/105884872

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值