分享

C#常用集合的使用

 雪柳花明 2016-10-30

大多数集合都在System.Collections,System.Collections.Generic两个命名空间。其中System.Collections.Generic专门用于泛型集合。

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

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

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

 

一、列表

[csharp] view plain copy
  1. [Serializable]  
  2. [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]  
  3. [DebuggerDisplay("Count = {Count}")]  
  4. public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable  


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

[csharp] view plain copy
  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.             List<String> list = new List<string>();  
  11.             list.Add("张三");  
  12.             list.Add("李四");  
  13.             list.Add("王五");  
  14.             list.Add("田六");  
  15.             list.Add("赵七");  
  16.   
  17.             for (int i = 0; i < list.Count; i++)  
  18.             {  
  19.                 Console.WriteLine("for循环:" + i.ToString() + "=" + list[i]);  
  20.             }  
  21.   
  22.             list.RemoveAt(0);  
  23.             foreach (String item in list)  
  24.             {  
  25.                 Console.WriteLine("foreach迭代:" + item);  
  26.             }  
  27.             list.AddRange(new String[] { "Hello1""Hello2""Hello3" });  
  28.   
  29.             list.ForEach(Print);  
  30.   
  31.             Console.Read();  
  32.         }  
  33.   
  34.         private static void Print(String item)  
  35.         {  
  36.             Console.WriteLine("ForEach:" + item);  
  37.         }  
  38.     }  
  39.   
  40. }  


 二、队列

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

[csharp] view plain copy
  1. [Serializable]  
  2. [DebuggerTypeProxy(typeof(System_QueueDebugView<>))]  
  3. [ComVisible(false)]  
  4. [DebuggerDisplay("Count = {Count}")]  
  5. public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable  

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


[csharp] view plain copy
  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.             Queue<String> queue = new Queue<string>();  
  11.             //进队  
  12.             queue.Enqueue("张三");  
  13.             queue.Enqueue("李四");  
  14.             queue.Enqueue("王五");  
  15.             queue.Enqueue("田六");  
  16.             queue.Enqueue("赵七");  
  17.   
  18.             foreach (String item in queue)  
  19.             {  
  20.                 Console.WriteLine("foreach迭代:" + item);  
  21.             }  
  22.   
  23.             //出队  
  24.             while (queue.Count > 0)  
  25.             {  
  26.                 Console.WriteLine("出队:" + queue.Dequeue());  
  27.             }  
  28.   
  29.             Console.Read();  
  30.         }  
  31.     }  
  32. }  


 三、栈

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

[csharp] view plain copy
  1. [DebuggerDisplay("Count = {Count}")]  
  2.    [DebuggerTypeProxy(typeof(System_StackDebugView<>))]  
  3.    [ComVisible(false)]  
  4.    public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable  


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

[csharp] view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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<intstring>();  
  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 plain copy
  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 plain copy
  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<intstring>();  
  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 plain copy
  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<intstring>();  
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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. }  


性能比较:

 

---------------------------------------------------------------------------------------------------------------------------


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多