• SortedList、SortedSet、HashSet、Hashtable、Dictionary、SortedDictionary 排序/可重复排序/过滤重复排序等简单对比


    //泛型的键值集合/有序/Hash算法/占内存较大/不排序,不受装填因子的限制,对读写操作效率较高
                Dictionary<int, string> dc = new Dictionary<int, string>();
                dc.Add(1, "111111");
                dc.Add(2, "222222");
                dc.Add(3, "333333");
                dc.Add(5, "5555555");
                dc.Add(4, "4444444");
                dc.Add(10, "101010");
                dc.Add(35, "353535");
    
                //泛型的键值集合/int排序
                SortedDictionary<int, string> sd = new SortedDictionary<int, string>();
                sd.Add(1, "111111");
                sd.Add(2, "222222");
                sd.Add(3, "333333");
                sd.Add(5, "5555555");
                sd.Add(4, "4444444");
                sd.Add(10, "101010");
                sd.Add(35, "353535");
    
                //泛型的键值集合/string排序
                SortedDictionary<string, string> sd2 = new SortedDictionary<string, string>();
                sd2.Add("1111", "aaaaa");
                sd2.Add("22222", "bbbbbb");
                sd2.Add("ccccc", "333333");
                sd2.Add("555555", "dddddd");
                sd2.Add("444444", "cccccc");
    
                //弱类型的键集合/无序/Hash算法/占内存较小/不排序,扩容时会对所有的数据需要重新进行散列计算,所以较适用于读取操作频繁,写入操作较少的操作类型
                Hashtable ht = new Hashtable();
                ht.Add(1, "111111");
                ht.Add(2, "222222");
                ht.Add(3, "333333");
                ht.Add(5, "5555555");
                ht.Add(4, "4444444");
                ht.Add(10, "101010");
                ht.Add(35, "353535");
    
                //弱类型的键集合/无序/Hash算法/占内存较小/不排序,扩容时会对所有的数据需要重新进行散列计算,所以较适用于读取操作频繁,写入操作较少的操作类型
                Hashtable ht2 = new Hashtable();
                ht2.Add("1111", "aaaaa");
                ht2.Add("22222", "bbbbbb");
                ht2.Add("ccccc", "333333");
                ht2.Add("555555", "dddddd");
                ht2.Add("444444", "cccccc");
    
                //范型int排序(使用内存比SortedDictionary少,对频繁插入移除操作较慢,比较适合排序数据一次性填充列表)
                SortedList<int, string> sl = new SortedList<int, string>();
                sl.Add(1, "111111");
                sl.Add(2, "222222");
                sl.Add(3, "333333");
                sl.Add(5, "5555555");
                sl.Add(4, "4444444");
                sl.Add(10, "101010");
                sl.Add(35, "353535");
    
                //范型string排序(使用内存比SortedDictionary少,对频繁插入移除操作较慢,比较适合排序数据一次性填充列表)
                SortedList<string, string> sl2 = new SortedList<string, string>();
                sl2.Add("1111", "aaaaa");
                sl2.Add("22222", "bbbbbb");
                sl2.Add("ccccc", "333333");
                sl2.Add("555555", "dddddd");
                sl2.Add("444444", "cccccc");
                //sl2.Add("ccccc", "333333");//相同Key不能加入
    
                //int类型/过滤重复/排序
                SortedSet<int> ss = new SortedSet<int>();
                ss.Add(1);
                ss.Add(2);
                ss.Add(3);
                ss.Add(5);
                ss.Add(4);
                ss.Add(10);
                ss.Add(35);
                ss.Add(5);//相同数据被过滤了(可以直接加,成功返回True,失败返回False)
    
                //int类型/过滤重复/不排序
                var set = new HashSet<int>() { 3, 8, 2, 1, 3, 3, 6, 8, 7, 2, 8 };
                //int类型/过滤重复/排序
                var set2 = new SortedSet<int> { 3, 8, 2, 1, 3, 3, 6, 8, 7, 2, 8 };
                //string类型/过滤重复/不排序
                var set3 = new HashSet<string>() { "3", "8", "2", "1", "3", "3", "6", "8", "7", "2", "8" };
                //string类型/过滤重复/排序
                var set4 = new SortedSet<string> { "3", "8", "2", "1", "3", "3", "6", "8", "7", "2", "8" };
    
                //过滤重复排序
                SortedSet<Person> ss2 = new SortedSet<Person>(new SortAge());
                ss2.Add(new Person { FirstName = "Homer", LastName = "Simpson", Age = 47 });
                ss2.Add(new Person { FirstName = "Marge", LastName = "Simpson", Age = 45 });
                ss2.Add(new Person { FirstName = "Lisa", LastName = "Simpson", Age = 9 });
                ss2.Add(new Person { FirstName = "Bart", LastName = "Simpson", Age = 8 });
                ss2.Add(new Person { FirstName = "Saku", LastName = "Simpson", Age = 1 });
                ss2.Add(new Person { FirstName = "Mikko", LastName = "Simpson", Age = 32 });
                ss2.Add(new Person { FirstName = "Bart2", LastName = "Simpson", Age = 8 });//被过滤了
    
                //不过滤重复排序
                List<Person> l = new List<Person>();
                l.Add(new Person { FirstName = "Homer", LastName = "Simpson", Age = 47 });
                l.Add(new Person { FirstName = "Marge", LastName = "Simpson", Age = 45 });
                l.Add(new Person { FirstName = "Lisa", LastName = "Simpson", Age = 9 });
                l.Add(new Person { FirstName = "Bart", LastName = "Simpson", Age = 8 });
                l.Add(new Person { FirstName = "Saku", LastName = "Simpson", Age = 1 });
                l.Add(new Person { FirstName = "Mikko", LastName = "Simpson", Age = 32 });
                l.Add(new Person { FirstName = "Bart2", LastName = "Simpson", Age = 8 });//不过滤
                l.Sort(new SortAge());

    Person 类代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }
        }
    
        public class SortAge : IComparer<Person>
        {
            public int Compare(Person firstPerson, Person secondPerson)
            {
                if (firstPerson.Age > secondPerson.Age)
                    return 1;
                if (firstPerson.Age < secondPerson.Age)
                    return -1;
                else
                    return 0;
            }
        }
    
    }

    转自:http://www.cnblogs.com/VvxT/archive/2012/08/23/2653485.html


  • 相关阅读:
    Network Simulator for P4(NSP4) src内容介绍
    解决 E: Unable to correct problems, you have held broken packages. 问题
    【旧版本】Ubuntu 14.04 下 P416编译器 p4c的安装
    Ubuntu 14.04 更新gcc版本至4.9.2
    Ubuntu 14.04 下 安装Protocol Buffers
    Ubuntu 14.04 删除软件附加依赖
    解决Floodlight界面无法显示问题
    OpenVirteX 创建简易虚拟网络
    2017年P4中国峰会北京站 会议小结
    406. Queue Reconstruction by Height
  • 原文地址:https://www.cnblogs.com/hanwest/p/2881886.html
Copyright © 2020-2023  润新知