溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中泛型集合的使用方法有哪些

發(fā)布時(shí)間:2022-10-24 09:15:16 來源:億速云 閱讀:160 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下C#中泛型集合的使用方法有哪些的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

SortedList<TKey, TValue>

SortedList<TKey, TValue>和List<T>比較相似,不同的地方在于SortedList集合元素是排過序的,往SortedList集合添加元素的時(shí)候需要添加鍵值對(duì)數(shù)據(jù)。在添加集合元素的時(shí)候,首先采用"二分查找算法"找到合適的位置,然后元素被放到該位置,該位置后面所有的集合元素整體后退一位。

        static void Main(string[] args)
        {
            var mySotedList = new SortedList<int, string>();
            mySotedList.Add(1, "張三");
            mySotedList.Add(2,"李四");
            mySotedList.Add(3,"趙六");
            mySotedList.Add(4,"王九");
            //判斷是否包含某個(gè)鍵
            mySotedList.ContainsKey(1);
            //判斷是否包含某個(gè)值
            mySotedList.ContainsValue("張三");
            //獲取某個(gè)鍵的索引
            int keyIndex = mySotedList.IndexOfKey(2);
            //獲取某個(gè)值的索引
            int valIndex = mySotedList.IndexOfValue("李四");
            //根據(jù)鍵刪除
            mySotedList.Remove(3);
            //根據(jù)索引刪除
            mySotedList.RemoveAt(1);
            //根據(jù)鍵查找元素
            string myVal = mySotedList[3];
            //根據(jù)索引查找元素
            string myVal1 = mySotedList.Values[1];
            //根據(jù)索引查找鍵
            int myKey = mySotedList.Keys[3];
            //獲取某個(gè)元素而不拋異常
            string myWantVal;
            if (mySotedList.TryGetValue(2, out myWantVal))
            {
                
            }
            //遍歷鍵
            foreach (int key in mySotedList.Keys)
            {
                
            }
            //遍歷值
            foreach (string str in mySotedList.Values)
            {
                
            }
        }

使用SortedList<TKey, TValue>相對(duì)不足的地方在于:當(dāng)插入數(shù)據(jù)的時(shí)候,是通過"二分查找算法"確定插入位置的,開銷相對(duì)昂貴;但,由于SortedList<TKey, TValue>集合元素是排過序的,這又讓查找變得方便。

如果需要對(duì)一個(gè)集合進(jìn)行經(jīng)常性的查找,而向集合插入數(shù)據(jù)相對(duì)不多,就可以考慮使用SortedList<TKey, TValue>。    

Dictionary<TKey,TValue>

Dictionary<TKey,TValue>把存儲(chǔ)的數(shù)據(jù)放在了一個(gè)HashTable中,每個(gè)集合元素的鍵必須是唯一的,且集合元素未經(jīng)排序。
與SortedList<TKey, TValue>相似的地方在于也是以鍵值對(duì)的形式添加集合元素的,不同的地方在于:插入數(shù)據(jù)的效率Dictionary<TKey,TValue>比SortedList<TKey, TValue>要高。

            var myDictionary = new Dictionary<int, string>();
            myDictionary.Add(1, "darren");
            myDictionary.Add(2, "jack");
            myDictionary.Add(3, "sunny");
            //根據(jù)鍵判斷是否存在
            myDictionary.ContainsKey(1);
            //根據(jù)值判斷是否存在
            myDictionary.ContainsValue("darren");
            //根據(jù)鍵刪除
            myDictionary.Remove(3);
            //根據(jù)鍵查找集合元素
            string myVal = myDictionary[2];
            //根據(jù)鍵查找元素不拋異常
            string myVal1;
            if (myDictionary.TryGetValue(2, out myVal1))
            {
                
            }
            //遍歷鍵
            foreach (int key in myDictionary.Keys)
            {
                
            }
            //遍歷值
            foreach (string value in myDictionary.Values)
            {
                
            }

SortedDictionary<TKey,TValue>

SortedDictionary<TKey,TValue>與SortedList<TKey, TValue>最大的不同在于:SortedDictionary<TKey,TValue>不允許通過索引獲取集合元素,其內(nèi)部維護(hù)著一個(gè)"紅黑樹",所以,當(dāng)執(zhí)行插入操作的時(shí)候,相比SortedList<TKey, TValue>,SortedDictionary<TKey,TValue>有更好的執(zhí)行效率,當(dāng)然也占用了更多的內(nèi)存。

           var mySortedDictionary = new SortedDictionary<int, string>();
            mySortedDictionary.Add(1,"one");
            mySortedDictionary.Add(2,"two");
            mySortedDictionary.Add(3,"three");
            //判斷是否包含某個(gè)鍵
            mySortedDictionary.ContainsKey(2);
            //判斷是否包含某個(gè)值
            mySortedDictionary.ContainsValue("two");
            //根據(jù)鍵移除某個(gè)元素
            mySortedDictionary.Remove(2);
            //根據(jù)鍵查找某個(gè)元素
            string myVal = mySortedDictionary[1];
            //根據(jù)鍵查找元素,不拋異常
            string myVal1;
            if (mySortedDictionary.TryGetValue(1, out myVal1))
            {
                
            }
            //遍歷所有鍵
            foreach (int key in mySortedDictionary.Keys)
            {
                
            }
            //遍歷所有值
            foreach (string val in mySortedDictionary.Values)
            {
                
            }

LinkedList<T>

每個(gè)集合元素都有屬性指向前一個(gè)和后一個(gè)節(jié)點(diǎn)。第一個(gè)的前一個(gè)節(jié)點(diǎn)是最后一個(gè)節(jié)點(diǎn),后一個(gè)節(jié)點(diǎn)是第二個(gè)節(jié)點(diǎn)。最后一個(gè)節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)是倒數(shù)第二個(gè)節(jié)點(diǎn),后一個(gè)節(jié)點(diǎn)是第一個(gè)節(jié)點(diǎn)。

           var myLinkedList = new LinkedList<string>();
            //創(chuàng)建第一個(gè)節(jié)點(diǎn)
            myLinkedList.AddFirst("one");
            //創(chuàng)建最后一個(gè)節(jié)點(diǎn)
           var lastNode = myLinkedList.AddLast("three");
            //在最后一個(gè)節(jié)點(diǎn)前添加一個(gè)節(jié)點(diǎn)
            myLinkedList.AddBefore(lastNode, "two");
            //在當(dāng)前最后一個(gè)節(jié)點(diǎn)后面添加一個(gè)節(jié)點(diǎn)
            myLinkedList.AddAfter(lastNode, "four");
            //遍歷節(jié)點(diǎn)值
            foreach (string value in myLinkedList)
            {
                
            }
            //根據(jù)值查找節(jié)點(diǎn)
            //找到符合條件的第一個(gè)節(jié)點(diǎn)
            var myNode = myLinkedList.Find("two");
            //根據(jù)值查找節(jié)點(diǎn)
            //找到符合條件的最后一個(gè)節(jié)點(diǎn)
            var myNode1 = myLinkedList.FindLast("one");
            //獲取第一個(gè)節(jié)點(diǎn)
            var firstNode = myLinkedList.First;
            //獲取最后一個(gè)節(jié)點(diǎn)
            var lastNode1 = myLinkedList.Last;
            //根據(jù)值產(chǎn)生節(jié)點(diǎn)
            myLinkedList.Remove("one");
            //刪除第一個(gè)節(jié)點(diǎn)
            myLinkedList.RemoveFirst();
            //刪除最后一個(gè)節(jié)點(diǎn)
            myLinkedList.RemoveLast();
            //清空所有節(jié)點(diǎn)
            myLinkedList.Clear();

如果對(duì)一個(gè)集合有很多的插入操作,或者插入批量集合元素,可以考慮使用LinkedList<T>。

SortedSet<T>

SortedSet<T>的內(nèi)部其實(shí)是SortedDictionary類型,但是沒有鍵,只有值。SortedSet代表一個(gè)抽象的數(shù)據(jù)集,數(shù)據(jù)集中的元素值必須是唯一的,未經(jīng)過排序的。

如果需要對(duì)一組數(shù)據(jù)進(jìn)行合并等操作,可以考慮使用SortedSet<T>。

            var sortedSet1 = new SortedSet<string>();
            sortedSet1.Add("one");
            sortedSet1.Add("two");
            sortedSet1.Add("three");
            var sortedSet2 = new SortedSet<string>();
            sortedSet2.Add("two");
            sortedSet2.Add("four");
            //判斷某個(gè)值是否存在
            sortedSet1.Contains("one");
            //合并數(shù)據(jù)集,取出重復(fù)項(xiàng)
            sortedSet1.ExceptWith(sortedSet2);
            //判斷一個(gè)數(shù)據(jù)集是否是另一個(gè)數(shù)據(jù)集的自己
            sortedSet1.IsSubsetOf(sortedSet2);
            //判斷一個(gè)i額數(shù)據(jù)集是否包含另一個(gè)數(shù)據(jù)集的所有元素
            sortedSet1.IsSubsetOf(sortedSet2);
            //判斷兩個(gè)數(shù)據(jù)集是否有交集
            sortedSet1.Overlaps(sortedSet2);
            //根據(jù)值刪除某個(gè)元素
            sortedSet1.Remove("two");
            //判斷兩個(gè)數(shù)據(jù)集是否相等
            sortedSet1.SetEquals(sortedSet2);
            //只包含在一個(gè)數(shù)據(jù)集中的元素,這些元素不包含在另一個(gè)數(shù)據(jù)集
            sortedSet1.SymmetricExceptWith(sortedSet2);
            //取兩個(gè)數(shù)據(jù)集的并集
            sortedSet1.UnionWith(sortedSet2);
            //獲取包含某些元素的數(shù)據(jù)集
            SortedSet<string> result = sortedSet1.GetViewBetween("one", "two");
            //遍歷數(shù)據(jù)集
            foreach (string val in sortedSet1)
            {
                
            }
            //清空數(shù)據(jù)集
            sortedSet1.Clear();

HashSet<T>

HashSet和SortedSet都實(shí)現(xiàn)了ISet接口。使用SortedSet的前提是:需要根據(jù)多個(gè)元素值作為查找條件;數(shù)據(jù)不能被哈希。其余情況應(yīng)考慮使用HashSet。

            var hashSet1 = new HashSet<string>();
            hashSet1.Add("one");
            hashSet1.Add("two");
            hashSet1.Add("three");
            var hashSet2 = new HashSet<string>();
            hashSet2.Add("two");
            hashSet2.Add("four");
            //判斷是否包含某個(gè)值
            hashSet1.Contains("four");
            //去掉一個(gè)數(shù)據(jù)集中與另一個(gè)數(shù)據(jù)集重復(fù)的項(xiàng)
            hashSet1.ExceptWith(hashSet2);
            //保留一個(gè)數(shù)據(jù)集中與另一個(gè)數(shù)據(jù)集相同的項(xiàng)
            hashSet1.IntersectWith(hashSet2);
            //判斷一個(gè)數(shù)據(jù)集是否是另一個(gè)數(shù)據(jù)集的子集
            hashSet1.IsSubsetOf(hashSet2);
            //判斷一個(gè)數(shù)據(jù)集是否包含另一個(gè)數(shù)據(jù)集的所有元素
            hashSet1.IsSupersetOf(hashSet2);
            //判斷兩個(gè)數(shù)據(jù)集是否有重復(fù)的元素
            hashSet1.Overlaps(hashSet2);
            //根據(jù)值刪除某個(gè)元素
            hashSet1.Remove("one");
            //判斷兩個(gè)數(shù)據(jù)集是否相等
            hashSet1.SetEquals(hashSet2);
            //合并兩個(gè)數(shù)據(jù)集
            hashSet1.UnionWith(hashSet1);
            //查找只包含在一個(gè)數(shù)據(jù)集中,卻不包含在另一個(gè)數(shù)據(jù)集的元素
            hashSet1.SymmetricExceptWith(hashSet2);
            //遍歷數(shù)據(jù)集
            foreach (string value in hashSet1)
            {
                
            }
            //清空數(shù)據(jù)集
            hashSet1.Clear();

這幾天體驗(yàn)了各個(gè)泛型集合的用法??偨Y(jié)如下:

Stack<T>先進(jìn)后出, 在這里。
Queue<T>先進(jìn)先出,在這里。

SortedList<TKey, TValue>,插入數(shù)據(jù)不是很頻繁,且經(jīng)常需要查找。
Dictionary<TKey,TValue>,經(jīng)常插入數(shù)據(jù),不排序,鍵值對(duì)。
SortedDictionary<TKey,TValue>,經(jīng)常插入數(shù)據(jù),排序,鍵值對(duì)。
LinkedList<T>,雙向鏈表,插入很隨意。
SortedSet<T>,數(shù)據(jù)集,根據(jù)多個(gè)元素值作為查找條件;數(shù)據(jù)不能被哈希。
HashSet<T>,數(shù)據(jù)集,大多數(shù)情況下使用HashSet處理數(shù)據(jù)集操作。

以上就是“C#中泛型集合的使用方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI