.NET总结--泛型与泛型集合,应用场景
泛型优点
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总结--泛型与泛型集合,应用场景的更多相关文章
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
内省的简单运用: JavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则. 采用遍历BeanInfo的所有属性方式来查找和 ...
- C#基础-hashtable,泛型和字典集合
hashtable 的存储方式 使用方法: 1.引入包含Hashtable的命名空间 using System.Collections; // 引入Hash所在的命名空间 2.往hash表里面添加数据 ...
- 【学习笔记】C#中的泛型和泛型集合
一.什么是泛型? 泛型是C#语言和公共语言运行库(CLR)中的一个新功能,它将类型参数的概念引入.NET Framework.类型参数使得设计某些类和方法成为可能,例如,通过使用泛型类型参数T,可以大 ...
- 通过反射 往泛型Integer的集合里添加String 类型的数据 Day25
package com.sxt.method1; import java.lang.reflect.Method; /* * 需求:通过反射 往泛型Integer的集合里添加String 类型的数据 ...
- java 面向对象(三十三):泛型二 泛型在集合中的使用
1. 在集合中使用泛型之前的例子 @Test public void test1(){ ArrayList list = new ArrayList(); //需求:存放学生的成绩 list.add( ...
- 深入理解什么是Java泛型?泛型怎么使用?【纯转】
本篇文章给大家带来的内容是介绍深入理解什么是Java泛型?泛型怎么使用?有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助. 一.什么是泛型 “泛型” 意味着编写的代码可以被不同类型的对象所 ...
- JavaSE学习总结(十六)—— 泛型与泛型应用
一.泛型概要 泛型(Generic)的本质是类型参数化,通俗的说就是用一个占位符来表示类型,这个类型可以是String,Integer等不确定的类型,表明可接受的类型. 泛型是Java中一个非常重要的 ...
- .NET泛型02,泛型的使用
在" .NET泛型01,为什么需要泛型,泛型基本语法"中,了解了泛型的基本概念,本篇偏重于泛型的使用.主要包括: ■ 泛型方法重载需要注意的问题■ 泛型的类型推断■ 泛型方法也可以 ...
- Java泛型:泛型的定义(类、接口、对象)、使用、继承
地址 http://blog.csdn.net/lirx_tech/article/details/51570138 1. 设计泛型的初衷: 1) 主要是为了解决Java容器无法记忆元素类型的问题 ...
- 泛型学习第四天——List泛型终结:什么是List泛型,泛型筛选,泛型排序
为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接将对象放入ArrayList,操作直观,但由于集合中的项是Object类型,因此每次使用都必须 ...
随机推荐
- C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别 (转载)
多维数组的声明 在声明时,必须指定数组的长度,格式为 type [lenght ,lenght ,lengh, ... ] , ]; 或声明时即赋值,由系统推断长度 int [,] test1 = { ...
- ABP 执行sql语句
由于业务繁琐,用EF比较麻烦,需要用到sql语句,然后网上找了很久,找到的例子都是老版本的,新版本有先声明已经去掉,不能用了 在这里做个小记 首先注入实例 private readonly IDbCo ...
- Ubuntu18.04 配置网卡、替换软件源
2019/10/29, Ubuntu Server 18.04 摘要:Ubuntu Server 18.04 采用netplan作为网络配置管理,修改IP使其连上网络,修改替换软件源 修改网卡配置 首 ...
- Content-Type属性的取值和作用
1.Content-Type 的值类型: 1.1 application/json:消息主体是序列化后的 JSON 字符串 1.2 application/x-www-form-urlencoded: ...
- jQuery选择器与过滤器(二)
一.jQuery选择器1.基本选择器:所有选择器 *标签选择器 标签名ID选择器 #ID类选择器 .className组合选择器 selector1,selector2 ...
- Python操作SQLite/MySQL/LMDB
1.概述 1.1前言 最近在存储字模图像集的时候,需要学习LMDB,趁此机会复习了SQLite和MySQL的使用,一起整理在此. 1.2环境 使用win7,Python 3.5.2. 2.SQLite ...
- Vue学习之动画小结(六)
一.Vue中实现动画的方式:https://cn.vuejs.org/v2/guide/transitions.html Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.包括 ...
- Android-----CheckBox复选使用(实现简单选餐)
直接上代码: xml布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...
- 摘jmeter安装之后如何汉化
下载完成后打开bin文件,选择jmeter.properties打开,搜索language,修改成zh_CN,汉化jmeter,记得去掉前面的#号,然后保存,修改完配置文件后需要重启jmeter 用的 ...
- python基础-os模块
os 模块 功能:与操作系统交互的模块 使用方式:import os 常用的几种功能 os.path.dirname(文件名) 用于获取当前文件的所在目录 import os # 获取当前文件的所在目 ...