集合

来源:互联网 发布:npc算法 编辑:程序博客网 时间:2024/06/02 07:33

 

1.数组

每个实例化的数组都继承于Array抽象类,Array抽象类实现了IEnumerable,ICollection,IList接口。其中不是每一个方法都实现了,没有实现的抛出异常。

2.List<T>,ArrayList

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

注意删除元素的时候,先看IEquatable接口,在看重写的Object的equals方法,再看引用。

AsReadOnly()可以返回只读集合,返回ReadOnlyCollection<T>与List<T>相同,只是有些写的方法抛出异常。

public class ArrayList : IList, ICollection, IEnumerable, ICloneable

3.Queue,Queue<T>

public class Queue : ICollection, IEnumerable, ICloneable

public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable

4.Stack,Stack<T>

public class Stack : ICollection, IEnumerable, ICloneable

public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable

5.LinkedList<T>

public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback

这是一个双向链表

6.SortedList<T>,SortedList

public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

这个集合在加入的时候就是排好序的。

7.Dictionary

public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

其中有一个构造函数,允许作为key的类型不实现equals和gethashcode,方法是构造函数传入一个IEqualityComparer<T>

TryGetValue(key,out value);如果有则返回true,没有则返回fasle。value取得值。

8.HashTable

查看与Dictionary的区别: http://blog.163.com/jeson_lwj/blog/static/13576108320101187546107/

1:多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
2:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
3:Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便. 

Hashtable类和 Dictionary<TKey, TValue> 泛型类实现 IDictionary 接口
Dictionary<TKey, TValue> 泛型类还实现 IDictionary<TKey, TValue>泛型接口。因此,这些集合中的每个元素都是一个键/值对。

Dictionary<TKey, TValue>类与 Hashtable 类的功能相同
对于值类型,特定类型(不包括 Object)的 Dictionary<TKey, TValue>的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作。



泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。 很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:
ArrayList List<T>
HashTable DIctionary<T>
Queue Queue<T>
Stack Stack<T>
SortedList SortedList<T>

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和拆箱的负担,如果我们操纵的数据类型相对确定的化  用 Dictionary<TKey,TValue> 集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用 Dictionary<string, int> 来存储购物车信息,而不需要任何的类型转化。

9.ListDictionary

和Hashtable类似,但是是用链表实现的。可以按顺序输出。当元素个数小于10个时,采用它比较快,否则Hashtable比较快

10.HybridDictionary

动态判断,当元素个数比较小的时候采用ListDictionary,当元素个数增大时改用Hashtable.事先不知道个数可以使用它

11.NameValueCollection

类似于有序的字符串值和字符串键的集合,它并没实现IDictionary接口。
string[] AllKeys属性:所有键的字符串数组。
bool HasKeys():集合对象中是否包含"键/值"对。
string Get(int index/string key):根据索引号或键名来取得元素的值。
string GetKey(int index):根据索引号取得键名。

12.SortedDictionary<T,V>

与SortedList类似,但是SortedList实现为一个基于数组的链表,但是它实现为字典。SortedList占用内存少,SortedDictionary的元素插入和删除比较快,在用已排好序的数据填充集合时,若不需要修改容量,SortedList<T,V>速度比较快

13.HashSet<T>

public class HashSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable, ISerializable, IDeserializationCallback

其中包含的方法中有一些集合操作。

14.LookUp<T,V>

类似于Dictionary<T,V>,不同的是值是一个V类型的集合,而不是一个V类型的值。不能主动创建,必须调用IEnumerable<T>的ToLookUp方法来获得。

 

除了Hashtable都是线程不安全的。位于System.Collections命名空间下的集合,如Hashtable,ArrayList,Stack,Queue等.其均提供了线程同步的一个实现。关于线程安全,请看

http://archive.cnblogs.com/a/1897534/

http://www.cnblogs.com/Mainz/archive/2008/04/06/CSharp_HashTable_Dictionary_ArrayList_Threadsafe.html

 

原创粉丝点击