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

摘要:
//通用键值集/排序/哈希算法/占用大量内存/未排序,并且不受加载因子的限制。它对读写操作非常有效。字典<int,string>dc=newDictionary<int,字符串>();直流。添加(1,“111111”);直流。增加(2,“222222”);直流。添加(3,“333333”);直流。添加(5,“5555555”);直流。添加(4,“4444444”);d
//泛型的键值集合/有序/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


免责声明:文章转载自《SortedList、SortedSet、HashSet、Hashtable、Dictionary、SortedDictionary 排序/可重复排序/过滤重复排序等简单对比》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Jquery获取列表中的值和input单选、多选框控制选中与取消nodeJS从入门到进阶二(网络部分)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

es版本2.x的string和5.x的keyword,text的区别和联系

一 es2.x和es5.x版本定义字符串类型 2.x版本的es string的类型 全文检索   分词   index=analysis  按单个字符匹配    被称作analyzed字符串 关键词搜索 不分词  index=not_analysis  按照整个文本进行匹配  被称为not-analyzed字符串 index=no  表示不被索引,产生的后...

itext生成pdf(附带页眉,页脚,页码)

packagecn.picclife.mwx.salesupport.marketactivity.util; importjava.io.File; importjava.io.FileOutputStream; importjava.io.IOException; importjava.net.MalformedURLException; impo...

java拷贝指定文件夹下的指定文件类型

例如:把C:WindowsSysWOW64下的所有dll文件拷贝到C:UsersAdministratorDesktop64dll这个目录 1 packagecom.xiaostudy.copyFile; 2 3 importjava.io.File; 4 importjava.io.FileInputStream; 5 importjava.i...

PHP5和PHP7引用对比(笔记)

php5在引入引用计数后,使用了refcount_gc来记录次数,同时使用is_ref_gc来记录是否是引用类型。 例如 $a = 'hello'; //$a->zval1(type=IS_STRING,refcount_gc=1,is_ref_gc=0) 这个时候$a指向一个结构体,主要看refcount_gc=1,这就是引用计数字段,因为hell...

JAVA基于图片相似性算法实现以图搜图样例

一、简述 本文主要讲如何利用图片相似性算法,基于LIRE来实现图片搜索。 二、依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core --> <dependency&...

TypeScript躬行记(8)——装饰器

  装饰器(Decorator)可声明在类及其成员(例如属性、方法等)之上,为它们提供一种标注,用于分离复杂逻辑或附加额外逻辑,其语法形式为@expression。expression是一个会在运行时被调用的函数,它的参数是被装饰的声明信息。假设有一个@sealed装饰器,那么可以像下面这样定义sealed()函数。 function sealed(tar...