本篇體驗除Queue<T>和Stack<T>之外的其它泛型集合。
SortedList<TKey, TValue>
SortedList<TKey, TValue>和List<T>比較相似,不同的地方在于SortedList集合元素是排過序的,往SortedList集合添加元素的時候需要添加鍵值對數據。在添加集合元素的時候,首先采用"二分查找算法"找到合適的位置,然后元素被放到該位置,該位置后面所有的集合元素整體后退一位。
static void Main(string[] args){var mySotedList = new SortedList<int, string>();mySotedList.Add(1, "張三");mySotedList.Add(2,"李四");mySotedList.Add(3,"趙六");mySotedList.Add(4,"王九");//判斷是否包含某個鍵mySotedList.ContainsKey(1);//判斷是否包含某個值mySotedList.ContainsValue("張三");//獲取某個鍵的索引int keyIndex = mySotedList.IndexOfKey(2);//獲取某個值的索引int valIndex = mySotedList.IndexOfValue("李四");//根據鍵刪除mySotedList.Remove(3);//根據索引刪除mySotedList.RemoveAt(1);//根據鍵查找元素string myVal = mySotedList[3];//根據索引查找元素string myVal1 = mySotedList.Values[1];//根據索引查找鍵int myKey = mySotedList.Keys[3];//獲取某個元素而不拋異常string myWantVal;if (mySotedList.TryGetValue(2, out myWantVal)){}//遍歷鍵foreach (int key in mySotedList.Keys){}//遍歷值foreach (string str in mySotedList.Values){}}
使用SortedList<TKey, TValue>相對不足的地方在于:當插入數據的時候,是通過"二分查找算法"確定插入位置的,開銷相對昂貴;但,由于SortedList<TKey, TValue>集合元素是排過序的,這又讓查找變得方便。
如果需要對一個集合進行經常性的查找,而向集合插入數據相對不多,就可以考慮使用SortedList<TKey, TValue>。
Dictionary<TKey,TValue>
Dictionary<TKey,TValue>把存儲的數據放在了一個HashTable中,每個集合元素的鍵必須是唯一的,且集合元素未經排序。
與SortedList<TKey, TValue>相似的地方在于也是以鍵值對的形式添加集合元素的,不同的地方在于:插入數據的效率Dictionary<TKey,TValue>比SortedList<TKey, TValue>要高。
var myDictionary = new Dictionary<int, string>();myDictionary.Add(1, "darren");myDictionary.Add(2, "jack");myDictionary.Add(3, "sunny");//根據鍵判斷是否存在myDictionary.ContainsKey(1);//根據值判斷是否存在myDictionary.ContainsValue("darren");//根據鍵刪除myDictionary.Remove(3);//根據鍵查找集合元素string myVal = myDictionary[2];//根據鍵查找元素不拋異常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>不允許通過索引獲取集合元素,其內部維護著一個"紅黑樹",所以,當執行插入操作的時候,相比SortedList<TKey, TValue>,SortedDictionary<TKey,TValue>有更好的執行效率,當然也占用了更多的內存。
var mySortedDictionary = new SortedDictionary<int, string>();mySortedDictionary.Add(1,"one");mySortedDictionary.Add(2,"two");mySortedDictionary.Add(3,"three");//判斷是否包含某個鍵mySortedDictionary.ContainsKey(2);//判斷是否包含某個值mySortedDictionary.ContainsValue("two");//根據鍵移除某個元素mySortedDictionary.Remove(2);//根據鍵查找某個元素string myVal = mySortedDictionary[1];//根據鍵查找元素,不拋異常string myVal1;if (mySortedDictionary.TryGetValue(1, out myVal1)){}//遍歷所有鍵foreach (int key in mySortedDictionary.Keys){}//遍歷所有值foreach (string val in mySortedDictionary.Values){}
LinkedList<T>
每個集合元素都有屬性指向前一個和后一個節點。第一個的前一個節點是最后一個節點,后一個節點是第二個節點。最后一個節點的前一個節點是倒數第二個節點,后一個節點是第一個節點。
var myLinkedList = new LinkedList<string>();//創建第一個節點myLinkedList.AddFirst("one");//創建最后一個節點var lastNode = myLinkedList.AddLast("three");//在最后一個節點前添加一個節點myLinkedList.AddBefore(lastNode, "two");//在當前最后一個節點后面添加一個節點myLinkedList.AddAfter(lastNode, "four");//遍歷節點值foreach (string value in myLinkedList){}//根據值查找節點//找到符合條件的第一個節點var myNode = myLinkedList.Find("two");//根據值查找節點//找到符合條件的最后一個節點var myNode1 = myLinkedList.FindLast("one");//獲取第一個節點var firstNode = myLinkedList.First;//獲取最后一個節點var lastNode1 = myLinkedList.Last;//根據值產生節點myLinkedList.Remove("one");//刪除第一個節點myLinkedList.RemoveFirst();//刪除最后一個節點myLinkedList.RemoveLast();//清空所有節點myLinkedList.Clear();
如果對一個集合有很多的插入操作,或者插入批量集合元素,可以考慮使用LinkedList<T>。
SortedSet<T>
SortedSet<T>的內部其實是SortedDictionary類型,但是沒有鍵,只有值。SortedSet代表一個抽象的數據集,數據集中的元素值必須是唯一的,未經過排序的。
如果需要對一組數據進行合并等操作,可以考慮使用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");//判斷某個值是否存在sortedSet1.Contains("one");//合并數據集,取出重復項sortedSet1.ExceptWith(sortedSet2);//判斷一個數據集是否是另一個數據集的自己sortedSet1.IsSubsetOf(sortedSet2);//判斷一個i額數據集是否包含另一個數據集的所有元素sortedSet1.IsSubsetOf(sortedSet2);//判斷兩個數據集是否有交集sortedSet1.Overlaps(sortedSet2);//根據值刪除某個元素sortedSet1.Remove("two");//判斷兩個數據集是否相等sortedSet1.SetEquals(sortedSet2);//只包含在一個數據集中的元素,這些元素不包含在另一個數據集sortedSet1.SymmetricExceptWith(sortedSet2);//取兩個數據集的并集sortedSet1.UnionWith(sortedSet2);//獲取包含某些元素的數據集SortedSet<string> result = sortedSet1.GetViewBetween("one", "two");//遍歷數據集foreach (string val in sortedSet1){}//清空數據集sortedSet1.Clear();
HashSet<T>
HashSet和SortedSet都實現了ISet接口。使用SortedSet的前提是:需要根據多個元素值作為查找條件;數據不能被哈希。其余情況應考慮使用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");//判斷是否包含某個值hashSet1.Contains("four");//去掉一個數據集中與另一個數據集重復的項hashSet1.ExceptWith(hashSet2);//保留一個數據集中與另一個數據集相同的項hashSet1.IntersectWith(hashSet2);//判斷一個數據集是否是另一個數據集的子集hashSet1.IsSubsetOf(hashSet2);//判斷一個數據集是否包含另一個數據集的所有元素hashSet1.IsSupersetOf(hashSet2);//判斷兩個數據集是否有重復的元素hashSet1.Overlaps(hashSet2);//根據值刪除某個元素hashSet1.Remove("one");//判斷兩個數據集是否相等hashSet1.SetEquals(hashSet2);//合并兩個數據集hashSet1.UnionWith(hashSet1);//查找只包含在一個數據集中,卻不包含在另一個數據集的元素hashSet1.SymmetricExceptWith(hashSet2);//遍歷數據集foreach (string value in hashSet1){}//清空數據集hashSet1.Clear();
這幾天體驗了各個泛型集合的用法。總結如下:
● Stack<T>先進后出, 在這里。
● Queue<T>先進先出,在這里。
● SortedList<TKey, TValue>,插入數據不是很頻繁,且經常需要查找。
● Dictionary<TKey,TValue>,經常插入數據,不排序,鍵值對。
● SortedDictionary<TKey,TValue>,經常插入數據,排序,鍵值對。
● LinkedList<T>,雙向鏈表,插入很隨意。
● SortedSet<T>,數據集,根據多個元素值作為查找條件;數據不能被哈希。
● HashSet<T>,數據集,大多數情況下使用HashSet處理數據集操作。
新聞熱點
疑難解答