星空网 > 软件开发 > ASP.net

C#各种泛型集合体验

 

本篇体验除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处理数据集操作。




原标题:C#各种泛型集合体验

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流