c#中經常使用集合類和集合接口之接口系列【轉】

經常使用集合接口系列http://www.cnblogs.com/fengxiaojiu/p/7997704.htmlhtml

經常使用集合類系列http://www.cnblogs.com/fengxiaojiu/p/7997541.htmlnode

大多數集合都在System.Collections,System.Collections.Generic兩個命名空間。其中System.Collections.Generic專門用於泛型集合。安全

針對特定類型的集合類型位於System.Collections.Specialized;命名空間;數據結構

線程安全的集合類位於System.Collections.Concurrent;命名空間。工具

下面是集合和列表實現的接口以下:性能

一:列表spa

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>實現了這麼多接口,具體接口的信息能夠經過工具查看。
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);
        }
    }

}

2、隊列.net

隊列先進先出,一頭進一頭出,用Queue<T>實現線程

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

能夠看出隊列實現了集合的接口,迭代的接口code

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();
        }
    }
}

3、棧

棧:從同一邊先進後出,用Stack<T>實現

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

棧也是實現了集合接口與迭代接口的

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Stack<String> stack = new Stack<string>();
            //進棧
            stack.Push("張三");
            stack.Push("李四");
            stack.Push("王五");
            stack.Push("田六");
            stack.Push("趙七");

            foreach (String item in stack)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            //出棧
            while (stack.Count > 0)
            {
                Console.WriteLine("出棧:" + stack.Pop());
            }

            Console.Read();
        }
    }
}

 4、鏈表

LinkedList是一個雙向鏈表,鏈表有個有點,就是在鏈表中間插入、刪除元素很快,可是查找中間與末尾的元素很慢,須要一個節點一個節點的去找。

    [Serializable]
    [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]
    public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback

因而可知鏈表也是有集合的特性的,能夠迭代,同時還有鏈表的特性

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            LinkedList<String> lList = new LinkedList<string>();
            LinkedListNode<String> node = new LinkedListNode<string>("root");
            lList.AddFirst(node);
            node = lList.AddAfter(node, "張三");
            node = lList.AddAfter(node, "李四");
            node = lList.AddAfter(node, "王五");
            node = lList.AddAfter(node, "田六");
            node = lList.AddAfter(node, "趙七");

            foreach (String item in lList)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            node = lList.First;
            Console.WriteLine("第一個元素:" + node.Value);
            node = lList.Last;
            Console.WriteLine("最後一個元素:" + node.Value);
            Console.Read();
        }
    }
}

 5、有序列表

SortedList採用鍵-值對存儲,鍵不能重複,而且會根據key進行排序

    [Serializable]
    [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]
    public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

能夠看出SortedList不只具備字典的特性,還有集合,迭代的功能

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Key必須惟一,若是不惟一能夠考慮Lookup<TKey,TElement>
            SortedList<int, String> sList = new SortedList<int, string>();
            sList.Add(100, "張三");
            sList.Add(21, "李四");
            sList.Add(13, "王五");
            sList.Add(44, "田六");
            sList.Add(35, "趙七");

            foreach (KeyValuePair<int, String> item in sList)
            {
                Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
            }

            Console.Read();
        }
    }
}

 6、字典

字典是很複雜的數據結構,容許經過key來查找值,字典能夠自由添加、刪除元素,沒有集合因爲移動元素致使的開銷。

[Serializable]
    [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]
    public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

能夠看出字典也具備集合的特性,能夠迭代

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Key必須惟一
            Dictionary<int, String> dict = new Dictionary<int, string>();
            dict.Add(11, "張三");
            dict.Add(1, "李四");
            dict.Add(2, "王五");
            dict.Add(16, "田六");
            dict.Add(12, "趙七");

            foreach (KeyValuePair<int, String> item in dict)
            {
                Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
            }

            Console.Read();
        }
    }
}

 說到字典,順便談一下有序字典,與有序列表對應;SortedDictionary,SortedList,SortedSet

會根據Key進行排序

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Key必須惟一
            SortedDictionary<int, String> dict = new SortedDictionary<int, string>();
            dict.Add(11, "張三");
            dict.Add(1, "李四");
            dict.Add(2, "王五");
            dict.Add(16, "田六");
            dict.Add(12, "趙七");

            foreach (KeyValuePair<int, String> item in dict)
            {
                Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
            }

            Console.Read();
        }
    }
}

7、集

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

 [Serializable]
    [DebuggerDisplay("Count = {Count}")]
    [DebuggerTypeProxy(typeof(HashSetDebugView<>))]
    public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable
 [Serializable]
    [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]
    [DebuggerDisplay("Count = {Count}")]
    public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback

 

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            HashSet<String> hSet = new HashSet<string>();
            hSet.Add("張三");
            hSet.Add("李四");
            hSet.Add("王五");
            hSet.Add("田六");
            hSet.Add("趙七");

            foreach (String item in hSet)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            Console.Read();
        }
    }
}

 

 

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            SortedSet<String> hSet = new SortedSet<string>();
            hSet.Add("張三");
            hSet.Add("李四");
            hSet.Add("王五");
            hSet.Add("田六");
            hSet.Add("趙七");

            foreach (String item in hSet)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            Console.Read();
        }
    }
}

性能比較:

 ps:原文連接:http://blog.csdn.net/ceclar123/article/details/8655853

相關文章
相關標籤/搜索