C# 各种集合

大多数集合都在  System.Collections,System.Collections.Generic两个命名空间。

其中System.Collections.Generic专门用于泛型集合。

针对特定类型的集合类型位于System.Collections.Specialized;命名空间;

线程安全的集合类位于System.Collections.Concurrent;命名空间。

下面是集合和列表实现的接口如下:

一、列表

[csharp] 
 
[Serializable]  
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]  

[DebuggerDisplay("Count = {Count}")]  

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


从这个可以看出,泛型集合List<T>实现了这么多接口,具体接口的信息可以通过工具查看。

 
using System;  
using System.Collections.Generic;  
  
namespace ConsoleApplication1  
{  
    public class Program  
    {  
        static void Main(string[] args)  
        {  
            List<String> list = new List<string>();  
            list.Add("张三");  
            list.Add("李四");  
            list.Add("王五");  
            list.Add("田六");  
            list.Add("赵七");  
  
            for (int i = 0; i < list.Count; i++)  
            {  
                Console.WriteLine("for循环:" + i.ToString() + "=" + list[i]);  
            }  
  
            list.RemoveAt(0);  
            foreach (String item in list)  
            {  
                Console.WriteLine("foreach迭代:" + item);  
            }  
            list.AddRange(new String[] { "Hello1", "Hello2", "Hello3" });  
  
            list.ForEach(Print);  
  
            Console.Read();  
        }  
  
        private static void Print(String item)  
        {  
            Console.WriteLine("ForEach:" + item);  
        }  
    }  
  
}  

  

 

 二、队列

队列先进先出,一头进一头出,用Queue<T>实现

[csharp]  
 
[Serializable]  
[DebuggerTypeProxy(typeof(System_QueueDebugView<>))]  
[ComVisible(false)]  
[DebuggerDisplay("Count = {Count}")]  
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable  

可以看出队列实现了集合的接口,迭代的接口

[csharp]  
 
using System;  
using System.Collections.Generic;  
  
namespace ConsoleApplication1  
{  
    public class Program  
    {  
        static void Main(string[] args)  
        {  
            Queue<String> queue = new Queue<string>();  
            //进队  
            queue.Enqueue("张三");  
            queue.Enqueue("李四");  
            queue.Enqueue("王五");  
            queue.Enqueue("田六");  
            queue.Enqueue("赵七");  
  
            foreach (String item in queue)  
            {  
                Console.WriteLine("foreach迭代:" + item);  
            }  
  
            //出队  
            while (queue.Count > 0)  
            {  
                Console.WriteLine("出队:" + queue.Dequeue());  
            }  
  
            Console.Read();  
        }  
    }  
}  
View Code


 三、栈

栈:从同一边先进后出,用Stack<T>实现

[csharp]  
 
[DebuggerDisplay("Count = {Count}")]  
   [DebuggerTypeProxy(typeof(System_StackDebugView<>))]  
   [ComVisible(false)]  
   public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable  

  


栈也是实现了集合接口与迭代接口的

[csharp]  
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Stack<String> stack = new Stack<string>();  
  11.             //进栈  
  12.             stack.Push("张三");  
  13.             stack.Push("李四");  
  14.             stack.Push("王五");  
  15.             stack.Push("田六");  
  16.             stack.Push("赵七");  
  17.   
  18.             foreach (String item in stack)  
  19.             {  
  20.                 Console.WriteLine("foreach迭代:" + item);  
  21.             }  
  22.   
  23.             //出栈  
  24.             while (stack.Count > 0)  
  25.             {  
  26.                 Console.WriteLine("出栈:" + stack.Pop());  
  27.             }  
  28.   
  29.             Console.Read();  
  30.         }  
  31.     }  
  32. }  


 四、链表

LinkedList是一个双向链表,链表有个有点,就是在链表中间插入、删除元素很快,但是查找中间与末尾的元素很慢,需要一个节点一个节点的去找。

[csharp] view plaincopy
 
  1. [Serializable]  
  2. [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]  
  3. [DebuggerDisplay("Count = {Count}")]  
  4. [ComVisible(false)]  
  5. public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback  

由此可见链表也是有集合的特性的,可以迭代,同时还有链表的特性

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             LinkedList<String> lList = new LinkedList<string>();  
  11.             LinkedListNode<String> node = new LinkedListNode<string>("root");  
  12.             lList.AddFirst(node);  
  13.             node = lList.AddAfter(node, "张三");  
  14.             node = lList.AddAfter(node, "李四");  
  15.             node = lList.AddAfter(node, "王五");  
  16.             node = lList.AddAfter(node, "田六");  
  17.             node = lList.AddAfter(node, "赵七");  
  18.   
  19.             foreach (String item in lList)  
  20.             {  
  21.                 Console.WriteLine("foreach迭代:" + item);  
  22.             }  
  23.   
  24.             node = lList.First;  
  25.             Console.WriteLine("第一个元素:" + node.Value);  
  26.             node = lList.Last;  
  27.             Console.WriteLine("最后一个元素:" + node.Value);  
  28.             Console.Read();  
  29.         }  
  30.     }  
  31. }  


 五、有序列表

SortedList采用键-值对存储,键不能重复,并且会根据key进行排序

[csharp] view plaincopy
 
  1. [Serializable]  
  2. [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]  
  3. [DebuggerDisplay("Count = {Count}")]  
  4. [ComVisible(false)]  
  5. public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable  

可以看出SortedList不仅具有字典的特性,还有集合,迭代的功能

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             //Key必须唯一,如果不唯一可以考虑Lookup<TKey,TElement>  
  11.             SortedList<int, String> sList = new SortedList<int, string>();  
  12.             sList.Add(100, "张三");  
  13.             sList.Add(21, "李四");  
  14.             sList.Add(13, "王五");  
  15.             sList.Add(44, "田六");  
  16.             sList.Add(35, "赵七");  
  17.   
  18.             foreach (KeyValuePair<int, String> item in sList)  
  19.             {  
  20.                 Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);  
  21.             }  
  22.   
  23.             Console.Read();  
  24.         }  
  25.     }  
  26. }  


 六、字典

字典是很复杂的数据结构,允许通过key来查找值,字典可以自由添加、删除元素,没有集合由于移动元素导致的开销。

[csharp] view plaincopy
 
  1. [Serializable]  
  2.     [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]  
  3.     [DebuggerDisplay("Count = {Count}")]  
  4.     [ComVisible(false)]  
  5.     public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback  

可以看出字典也具有集合的特性,可以迭代

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             //Key必须唯一  
  11.             Dictionary<int, String> dict = new Dictionary<int, string>();  
  12.             dict.Add(11, "张三");  
  13.             dict.Add(1, "李四");  
  14.             dict.Add(2, "王五");  
  15.             dict.Add(16, "田六");  
  16.             dict.Add(12, "赵七");  
  17.   
  18.             foreach (KeyValuePair<int, String> item in dict)  
  19.             {  
  20.                 Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);  
  21.             }  
  22.   
  23.             Console.Read();  
  24.         }  
  25.     }  
  26. }  


 说到字典,顺便谈一下有序字典,与有序列表对应;SortedDictionary,SortedList,SortedSet

会根据Key进行排序

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             //Key必须唯一  
  11.             SortedDictionary<int, String> dict = new SortedDictionary<int, string>();  
  12.             dict.Add(11, "张三");  
  13.             dict.Add(1, "李四");  
  14.             dict.Add(2, "王五");  
  15.             dict.Add(16, "田六");  
  16.             dict.Add(12, "赵七");  
  17.   
  18.             foreach (KeyValuePair<int, String> item in dict)  
  19.             {  
  20.                 Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);  
  21.             }  
  22.   
  23.             Console.Read();  
  24.         }  
  25.     }  
  26. }  

 七、集

集(Set):包含不重复元素,常用HashSet,SortedSet

[csharp] view plaincopy
 
  1. [Serializable]  
  2.    [DebuggerDisplay("Count = {Count}")]  
  3.    [DebuggerTypeProxy(typeof(HashSetDebugView<>))]  
  4.    public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable  
[csharp] view plaincopy
 
  1. [Serializable]  
  2.    [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]  
  3.    [DebuggerDisplay("Count = {Count}")]  
  4.    public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback  


 

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             HashSet<String> hSet = new HashSet<string>();  
  11.             hSet.Add("张三");  
  12.             hSet.Add("李四");  
  13.             hSet.Add("王五");  
  14.             hSet.Add("田六");  
  15.             hSet.Add("赵七");  
  16.   
  17.             foreach (String item in hSet)  
  18.             {  
  19.                 Console.WriteLine("foreach迭代:" + item);  
  20.             }  
  21.   
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. }  
[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     public class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             SortedSet<String> hSet = new SortedSet<string>();  
  11.             hSet.Add("张三");  
  12.             hSet.Add("李四");  
  13.             hSet.Add("王五");  
  14.             hSet.Add("田六");  
  15.             hSet.Add("赵七");  
  16.   
  17.             foreach (String item in hSet)  
  18.             {  
  19.                 Console.WriteLine("foreach迭代:" + item);  
  20.             }  
  21.   
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. }  


性能比较:

出自:http://blog.csdn.net/ceclar123/article/details/8655853

原文地址:https://www.cnblogs.com/sunqiang/p/4444235.html