泛型优点

  1.提高代码复用性,代码简洁直观

  2.直接存储数据类型免去数据类型之间得隐式转换

  3.免去拆箱装箱过程,提高效率

  4.数据类型安全,存储时会验证是否对应该类型

泛型集合

  一. ArrayList与Array与List<T>

  1.ArrayList属于自增容器,也就是无需定义其长度可直接使用而Array需要定义其长度

  2.ArrayList包含操作某范围元素方法而Array只能获取一个或设置一个元素得值

  3.ArrayList可以轻松创建同步版本,而Array需要手动更新

  4.ArrayList需引用System.Collections方可使用而Array只需System即可

  5.已知特定类型(Object除外)Array要比ArrayList好用

  6.ArrayList与List<T>类型相似,但后者类型更加安全而且无需拆装箱操作便可直接使用

  7.ArrayList没有类型约束而List<T>需要约束类型

  

ArrayList arrayList1 = new ArrayList();
arrayList1.
arrayList1.Add("a");
arrayList1.Add();
arrayList1.Add("b");
Response.Write(arrayList1[]);

  

List < Student > students = new List < Student > ();
Student stu1 = new Student();
stu1.Name = "陆小凤";
stu1.Number = "";
stu1.Score = ;
Student stu2 = new Student();
stu2.Name = "西门吹雪";
stu2.Number = "";
stu2.Score = ;
students.Add(stu1);
students.Add(stu2);
Console.WriteLine("集合中的元素个数为{0}", students.Count);
foreach (Student stu in students)
{
Console.WriteLine("/t{0}/t{1}/t{2}", stu.Name, stu.Number, stu.Score);
}
students.Remove(stu1);
Console.WriteLine("集合中的元素个数为{0}", students.Count);
Console.ReadLine();

  

  二.  HashTable 与 Dictionary<key,value>类型

  1.单线程程序推荐Dictionary,读写速度快,容量利用更加便利

  2.多线程程序推荐HashTable,可以单线程写入,多线程读取

  3.Dictionary非线程安全,所以在使用时需要lock一下保护当前语句执行完成,而且二者都实现IDictionary接口,所以他们一般都是键值对

  4.Dictionary为按序插入数据队列若其中节点被删除后 顺序会被打乱

  5.HashTable 元素属于 Object类型所以在操作时经常发生装箱拆箱操作效率低于Dictionary

  深入解析:

    HashTable

    1.其本质是键值对形式存储,还有一个类似索引的值由HashCode的值类似索引值作为该数据位置索引

    2.GetHashCode可以获得尽量不会重复的值来作为该数据存储地址.这就是散列函数GetHashCode的作用

    3.当HashTable呗占用大半的时候GetHashCode可能会得到重复地址,这就是哈希冲突

    4.键值对在HashTable中的位置是由Position = (HashCode&0X7FFFFFFF)%HashTable.Length 来确定的

    5..NET中是用探测方法来解决哈希冲突的,当Position+x如果存在则位移至Position+2*x位置,所以HashTable占用越多计算时间越长存储速度越慢

    6.HashTable中当达到当前空间72%时会出现自动扩容,例如空间大小是100当位置占用到73的时候该空间会自动扩容

    Dictionary

    1.Dictionary是一种变种的HashTable主要是解决哈希冲突方式不同

  三. 功能对比

    测试代码属于摘抄:

    

public class HashTableTest

    {

        static Hashtable _Hashtable;

        static Dictionary<string, object> _Dictionary;

        static void Main()

        {

            Compare();

            Compare();

            Compare();

            Console.ReadLine();

        }

        public static void Compare(int dataCount)

        {

            Console.WriteLine("-------------------------------------------------\n");

            _Hashtable = new Hashtable();

            _Dictionary = new Dictionary<string, object>();

            Stopwatch stopWatch = new Stopwatch();

            //HashTable插入dataCount条数据需要时间

            stopWatch.Start();

            for (int i = ; i < dataCount; i++)

            {

                _Hashtable.Add("Str" + i.ToString(), "Value");

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable插入" + dataCount + "条数据需要时间:" + stopWatch.Elapsed);

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            stopWatch.Start();

            for (int i = ; i < dataCount; i++)

            {

                _Dictionary.Add("Str" + i.ToString(), "Value");

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary插入" + dataCount + "条数据需要时间:" + stopWatch.Elapsed);

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            int si = ;

            stopWatch.Start();

            for(int i=;i<_Hashtable.Count;i++)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable遍历时间:" + stopWatch.Elapsed + " ,遍历采用for方式");

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            si = ;

            stopWatch.Start();

            foreach (var s in _Hashtable)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable遍历时间:" + stopWatch.Elapsed + " ,遍历采用foreach方式");

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            si = ;

            stopWatch.Start();

            IDictionaryEnumerator _hashEnum = _Hashtable.GetEnumerator();

            while (_hashEnum.MoveNext())

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable遍历时间:" + stopWatch.Elapsed + " ,遍历采用HashTable.GetEnumerator()方式");

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            si = ;

            stopWatch.Start();

            for(int i=;i<_Dictionary.Count;i++)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary遍历时间:" + stopWatch.Elapsed + " ,遍历采用for方式");

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            si = ;

            stopWatch.Start();

            foreach (var s in _Dictionary)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary遍历时间:" + stopWatch.Elapsed + " ,遍历采用foreach方式");

            //Dictionary插入dataCount条数据需要时间

            stopWatch.Reset();

            si = ;

            stopWatch.Start();

            _hashEnum = _Dictionary.GetEnumerator();

            while (_hashEnum.MoveNext())

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary遍历时间:" + stopWatch.Elapsed + " ,遍历采用Dictionary.GetEnumerator()方式");

            Console.WriteLine("\n-------------------------------------------------");

        }

    }

    综上测试可知:

    1.大量数据拆入时HashTable要比Dictionary慢很多

    2.for方式遍历HashTable和Dictionary速度最快

    3.foreach便利Dictionary时候花费时间更短

  三.  Queue和Stack

  1.Queue与Stack区别在于前者存储于队列,后者存储在栈中

  2.Queue属于先进先出,Stack属于先进后出 注意堆栈关系

  四.  SortedList

  1.引用System.Collections.SortList命名空间,其内部存储相当于两个数组 键数组与值数组 二者相关联关系,其中可以通过键来查询值,也可以根据值来搜索键,但是键不能为空,而值可以

  2.属于自增集合,可以根据自身数据大小来增加占用空间,也可以通过属性设置来减少空间

  3.若集合不再新增数据可以通过方法来设置该集合占用大小,注意该集合不会自动减少占用空间

  4.若输入键未存储则默认新增该键对应值数据,若该键对应值已有数据则默认覆盖原数据

  5.若不确定该键是否存在可以根据方法返回值判定是否存在

  泛型集合SortedList<key,value>例子:

  

static void Main(string[] args)
. {
. // 创建一个SortedList对象
. SortedList mySortedList = new SortedList();
. mySortedList.Add("First", "Hello");
. mySortedList.Add("Second", "World");
. mySortedList.Add("Third", "!");
. mySortedList.Add("Four", "{1}quot;);
.
. //列举SortedList的属性、键、值
. Console.WriteLine("MySortedList");
. Console.WriteLine(" Count: {0}", mySortedList.Count);
. Console.WriteLine(" Capacity: {0}", mySortedList.Capacity);
. Console.WriteLine(" Keys and Values:");
. PrintIndexAndKeysAndValues(mySortedList);
.
. #region SortedList获得键、值列表
. SortedList mySortedList1 = new SortedList();
. mySortedList1.Add(1.3, "fox");
. mySortedList1.Add(1.4, "jumped");
. mySortedList1.Add(1.5, "over");
. mySortedList1.Add(1.2, "brown");
. mySortedList1.Add(1.1, "quick");
. mySortedList1.Add(1.0, "The");
. mySortedList1.Add(1.6, "the");
. mySortedList1.Add(1.8, "dog");
. mySortedList1.Add(1.7, "lazy");
.
. //获得指定索引处的键和值
. int myIndex = ;
. // 获取 System.Collections.SortedList 对象的指定索引处的键
. Console.WriteLine("The key at index {0} is {1}.", myIndex, mySortedList1.GetKey(myIndex));
. // 获取 System.Collections.SortedList 对象的指定索引处的值
. Console.WriteLine("The value at index {0} is {1}.", myIndex, mySortedList1.GetByIndex(myIndex));
.
. // 获得SortedList中的键列表和值列表
. IList myKeyList = mySortedList1.GetKeyList();
. IList myValueList = mySortedList1.GetValueList();
. // Prints the keys in the first column and the values in the second column.
. Console.WriteLine("\t-KEY-\t-VALUE-");
. for (int i = ; i < mySortedList1.Count; i++)
. Console.WriteLine("\t{0}\t{1}", myKeyList[i], myValueList[i]);
.
. #endregion
.
. #region 为SortedList中的元素重新赋值
. // Creates and initializes a new SortedList.
. SortedList mySortedList2 = new SortedList();
. mySortedList2.Add(, "two");
. mySortedList2.Add(, "three");
. mySortedList2.Add(, "one");
. mySortedList2.Add(, "zero");
. mySortedList2.Add(, "four");
. // 打印显示列表的键和值
. Console.WriteLine("The SortedList contains the following values:");
. PrintIndexAndKeysAndValues(mySortedList2);
.
. // 获得指定键的索引
. int myKey = ;
. Console.WriteLine("The key \"{0}\" is at index {1}.", myKey, mySortedList2.IndexOfKey(myKey));
. // 获得指定值的索引
. String myValue = "three";
. Console.WriteLine("The value \"{0}\" is at index {1}.", myValue, mySortedList2.IndexOfValue(myValue));
. // 重新设置指定索引处的值
. mySortedList2.SetByIndex(, "III"); // SetByIndex:替换 System.Collections.SortedList 对象中指定索引处的值
. mySortedList2.SetByIndex(, "IV");
. //打印显示列表的键和值
. Console.WriteLine("After replacing the value at index 3 and index 4,");
. PrintIndexAndKeysAndValues(mySortedList2);
. #endregion
. Console.ReadKey();
. }
.
. //打印SortedList中的键和值
. public static void PrintIndexAndKeysAndValues(SortedList myList)
. {
. Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
. for (int i = ; i < myList.Count; i++)
. {
. Console.WriteLine("\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i));
. }
. Console.WriteLine();
. }
. }
static void Main(string[] args)
. {
. // 创建一个SortedList对象
. SortedList mySortedList = new SortedList();
. mySortedList.Add("First", "Hello");
. mySortedList.Add("Second", "World");
. mySortedList.Add("Third", "!");
. mySortedList.Add("Four", "{1}quot;);
.
. //列举SortedList的属性、键、值
. Console.WriteLine("MySortedList");
. Console.WriteLine(" Count: {0}", mySortedList.Count);
. Console.WriteLine(" Capacity: {0}", mySortedList.Capacity);
. Console.WriteLine(" Keys and Values:");
. PrintIndexAndKeysAndValues(mySortedList);
.
. #region SortedList获得键、值列表
. SortedList mySortedList1 = new SortedList();
. mySortedList1.Add(1.3, "fox");
. mySortedList1.Add(1.4, "jumped");
. mySortedList1.Add(1.5, "over");
. mySortedList1.Add(1.2, "brown");
. mySortedList1.Add(1.1, "quick");
. mySortedList1.Add(1.0, "The");
. mySortedList1.Add(1.6, "the");
. mySortedList1.Add(1.8, "dog");
. mySortedList1.Add(1.7, "lazy");
.
. //获得指定索引处的键和值
. int myIndex = ;
. // 获取 System.Collections.SortedList 对象的指定索引处的键
. Console.WriteLine("The key at index {0} is {1}.", myIndex, mySortedList1.GetKey(myIndex));
. // 获取 System.Collections.SortedList 对象的指定索引处的值
. Console.WriteLine("The value at index {0} is {1}.", myIndex, mySortedList1.GetByIndex(myIndex));
.
. // 获得SortedList中的键列表和值列表
. IList myKeyList = mySortedList1.GetKeyList();
. IList myValueList = mySortedList1.GetValueList();
. // Prints the keys in the first column and the values in the second column.
. Console.WriteLine("\t-KEY-\t-VALUE-");
. for (int i = ; i < mySortedList1.Count; i++)
. Console.WriteLine("\t{0}\t{1}", myKeyList[i], myValueList[i]);
.
. #endregion
.
. #region 为SortedList中的元素重新赋值
. // Creates and initializes a new SortedList.
. SortedList mySortedList2 = new SortedList();
. mySortedList2.Add(, "two");
. mySortedList2.Add(, "three");
. mySortedList2.Add(, "one");
. mySortedList2.Add(, "zero");
. mySortedList2.Add(, "four");
. // 打印显示列表的键和值
. Console.WriteLine("The SortedList contains the following values:");
. PrintIndexAndKeysAndValues(mySortedList2);
.
. // 获得指定键的索引
. int myKey = ;
. Console.WriteLine("The key \"{0}\" is at index {1}.", myKey, mySortedList2.IndexOfKey(myKey));
. // 获得指定值的索引
. String myValue = "three";
. Console.WriteLine("The value \"{0}\" is at index {1}.", myValue, mySortedList2.IndexOfValue(myValue));
. // 重新设置指定索引处的值
. mySortedList2.SetByIndex(, "III"); // SetByIndex:替换 System.Collections.SortedList 对象中指定索引处的值
. mySortedList2.SetByIndex(, "IV");
. //打印显示列表的键和值
. Console.WriteLine("After replacing the value at index 3 and index 4,");
. PrintIndexAndKeysAndValues(mySortedList2);
. #endregion
. Console.ReadKey();
. }
.
. //打印SortedList中的键和值
. public static void PrintIndexAndKeysAndValues(SortedList myList)
. {
. Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
. for (int i = ; i < myList.Count; i++)
. {
. Console.WriteLine("\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i));
. }
. Console.WriteLine();
. }
. }

.NET总结--泛型与泛型集合,应用场景的更多相关文章

  1. Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合

    内省的简单运用: JavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则. 采用遍历BeanInfo的所有属性方式来查找和 ...

  2. C#基础-hashtable,泛型和字典集合

    hashtable 的存储方式 使用方法: 1.引入包含Hashtable的命名空间 using System.Collections; // 引入Hash所在的命名空间 2.往hash表里面添加数据 ...

  3. 【学习笔记】C#中的泛型和泛型集合

    一.什么是泛型? 泛型是C#语言和公共语言运行库(CLR)中的一个新功能,它将类型参数的概念引入.NET Framework.类型参数使得设计某些类和方法成为可能,例如,通过使用泛型类型参数T,可以大 ...

  4. 通过反射 往泛型Integer的集合里添加String 类型的数据 Day25

    package com.sxt.method1; import java.lang.reflect.Method; /* * 需求:通过反射 往泛型Integer的集合里添加String 类型的数据 ...

  5. java 面向对象(三十三):泛型二 泛型在集合中的使用

    1. 在集合中使用泛型之前的例子 @Test public void test1(){ ArrayList list = new ArrayList(); //需求:存放学生的成绩 list.add( ...

  6. 深入理解什么是Java泛型?泛型怎么使用?【纯转】

    本篇文章给大家带来的内容是介绍深入理解什么是Java泛型?泛型怎么使用?有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助. 一.什么是泛型 “泛型” 意味着编写的代码可以被不同类型的对象所 ...

  7. JavaSE学习总结(十六)—— 泛型与泛型应用

    一.泛型概要 泛型(Generic)的本质是类型参数化,通俗的说就是用一个占位符来表示类型,这个类型可以是String,Integer等不确定的类型,表明可接受的类型. 泛型是Java中一个非常重要的 ...

  8. .NET泛型02,泛型的使用

    在" .NET泛型01,为什么需要泛型,泛型基本语法"中,了解了泛型的基本概念,本篇偏重于泛型的使用.主要包括: ■ 泛型方法重载需要注意的问题■ 泛型的类型推断■ 泛型方法也可以 ...

  9. Java泛型:泛型的定义(类、接口、对象)、使用、继承

    地址   http://blog.csdn.net/lirx_tech/article/details/51570138 1. 设计泛型的初衷: 1) 主要是为了解决Java容器无法记忆元素类型的问题 ...

  10. 泛型学习第四天——List泛型终结:什么是List泛型,泛型筛选,泛型排序

    为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接将对象放入ArrayList,操作直观,但由于集合中的项是Object类型,因此每次使用都必须 ...

随机推荐

  1. Linux学习笔记之vim操作指令大全

    0x00 关于Vim Vim是款强大的文本编辑器,但是众多指令需要学习,这次记录了指令大全方便以后翻阅. Vim的几种模式 正常模式:可以使用快捷键命令,或按:输入命令行. 插入模式:可以输入文本,在 ...

  2. JSON数据格式:以及XML文件格式,YML文件格式,properties文件格式

    JSON数据格式:以及XML文件格式,YML文件格式,properties文件格式   数据格式: json数据格式:属于轻量级数据格式,是javascript的一种描述数据的格式.具有易于解析,语法 ...

  3. System.ArgumentException:路由集合中已存在名为“XXX”的路由。路由名称必须唯一。

    软件环境:Visual Studio 2017 + MVC4 + EF6 问题描述:System.ArgumentException:路由集合中已存在名为“XXX”的路由.路由名称必须唯一. 解决办法 ...

  4. 如何在JIRA中有效使用关注和@提及 我正在关注的问题 提及我的问题 在仪表板上显示

    如何在JIRA中有效使用关注和@提及http://bbs.51testing.com/forum.php?mod=viewthread&tid=1157043&fromuid=1530 ...

  5. Java自学-数字与字符串 MyStringBuffer

    自己开发一个Java StringBuffer 根据接口IStringBuffer ,自己做一个MyStringBuffer 步骤 1 : IStringBuffer接口 package charac ...

  6. 数据库-如何创建SQL Server身份验证用户

    1.简介 默认安装SQL Server数据库后,SQL Server通过工具SQL Server Management Studio(SSMS)采用“Windows身份验证”方式登录,需要设置相应用户 ...

  7. Oracle数据库主外键 级联删除记录

    /** * 1. NO ACTION :指当删除主表中被引用列的数据时,如果子表的引用列中包含该值,则禁止该操作执行. * * 2. SET NULL :指当删除主表中被引用列的数据时,将子表中相应引 ...

  8. prometheus学习系列五: Prometheus配置文件

    在prometheus监控系统,prometheus的职责是采集,查询和存储和推送报警到alertmanager.本文主要介绍下prometheus的配置文件. 全局配置文件简介 默认配置文件 [ro ...

  9. 浅谈Python设计模式 - 工厂模式

    声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 工厂模式: 顾名思义,工厂则是根据提供的不同的材料,生产出不同的产品.那么在编程 ...

  10. css overflow失效的原因

    声明 转载自https://my.oschina.net/xuqianwen/blog/540587 项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分 ...