当前位置: 首页 > news >正文

了解一下C#的SortedSet

基础概念

SortedSet 是 C# 中的一个集合类型,位于 System.Collections.Generic 命名空间下。它是一个自动排序的集合,用于存储不重复的元素,并且会根据元素的自然顺序(默认排序)或自定义比较器进行排序,内部使用红黑树数据结构来维护元素的有序性。

  • 自动排序:每次添加或删除元素时,SortedSet 都会自动调整以保持元素的排序状态。
  • 不重复元素:SortedSet 不允许重复的元素。如果尝试添加一个已经存在的元素,该操作会被忽略。
  • 高效性:SortedSet 内部使用红黑树(一种自平衡二叉搜索树)实现,因此查找、插入和删除操作的时间复杂度为 O(log n)

主要特性

  • 自动保持元素排序:元素会根据其自然顺序(需实现 IComparable<T> 接口)或自定义比较器(IComparer<T>)排序。
  • 不包含重复元素:尝试添加已有元素时,Add 方法返回 false,集合保持不变。
  • 支持集合操作:提供并集、交集、差集等操作。
  • 支持子集视图:可以通过方法获取某个范围内的元素。
  • 快速访问边界值:提供 Min 和 Max 属性,快速获取最小和最大元素。

创建和初始化

基本创建方式

  • 使用默认比较器(升序)
    • // 使用默认比较器(升序)
      SortedSet<int> numbers = new SortedSet<int>();
  •  使用自定义比较器
    • // 使用自定义比较器
      SortedSet<string> names = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
  • 从现有集合创建
    • // 从现有集合创建
      int[] array = { 5, 2, 8, 1, 9 };
      SortedSet<int> sortedNumbers = new SortedSet<int>(array);
      // 结果:{1, 2, 5, 8, 9}
  • 使用集合初始化器
    • // 使用集合初始化器
      SortedSet<string> fruits = new SortedSet<string> { "Apple", "Banana", "Cherry" };

自定义比较器

  • 降序排列
    • // 降序排列
      SortedSet<int> descendingNumbers = new SortedSet<int>(Comparer<int>.Create((x, y) => y.CompareTo(x)));
  • 自定义对象排序
    • // 自定义对象排序
      public class Person : IComparable<Person>
      {public string Name { get; set; }public int Age { get; set; }public int CompareTo(Person other){if (other == null) return 1;return this.Age.CompareTo(other.Age); // 按年龄排序}
      }SortedSet<Person> people = new SortedSet<Person>();
  • 使用自定义比较器
    • // 或使用自定义比较器
      SortedSet<Person> peopleByName = new SortedSet<Person>(Comparer<Person>.Create((p1, p2) => string.Compare(p1.Name, p2.Name))
      );

基本操作

添加和删除元素

SortedSet<int> numbers = new SortedSet<int>();
  • 添加元素
    • // 添加元素
      bool added1 = numbers.Add(5);    // true,成功添加
      bool added2 = numbers.Add(3);    // true,成功添加
      bool added3 = numbers.Add(5);    // false,元素已存在Console.WriteLine(string.Join(", ", numbers)); // 输出:3, 5
  • 删除元素
    • // 删除元素
      bool removed = numbers.Remove(3); // true,成功删除
      numbers.Remove(10);  
  • 清空集合
    • // 清空集合
      numbers.Clear();

查询操作

SortedSet<int> numbers = new SortedSet<int> { 1, 3, 5, 7, 9 };
  • 检查元素是否存在
    • // 检查元素是否存在
      bool contains = numbers.Contains(5); // true
  • 获取元素数量
    • // 获取元素数量
      int count = numbers.Count; // 5
  • 检查是否为空
    • // 检查是否为空
      bool isEmpty = numbers.Count == 0; // false
  • 获取最小值和最大值
    • // 获取最小值和最大值
      int min = numbers.Min; // 1
      int max = numbers.Max; // 9

范围查询

  • 使用 GetViewBetween 方法获取指定范围内的元素子集
  • SortedSet<int> numbers = new SortedSet<int> { 1, 3, 5, 7, 9, 11, 13 };// 获取视图(不创建新集合)
    SortedSet<int> subset1 = numbers.GetViewBetween(3, 9);
    // 结果:{3, 5, 7, 9}SortedSet<int> subset2 = numbers.GetViewBetween(4, 10);
    // 结果:{5, 7, 9}// 视图会反映原集合的变化
    numbers.Add(6);
    Console.WriteLine(string.Join(", ", subset2)); // 输出:5, 6, 7, 9

    集合运算

    并集、交集、差集

SortedSet<int> set1 = new SortedSet<int> { 1, 2, 3, 4, 5 };
SortedSet<int> set2 = new SortedSet<int> { 4, 5, 6, 7, 8 };
  • 并集:UnionWith 将另一个集合的元素合并到 SortedSet 中。
    • // 并集(修改 set1)
      set1.UnionWith(set2);
      Console.WriteLine(string.Join(", ", set1)); // 1, 2, 3, 4, 5, 6, 7, 8
  • 交集:IntersectWith 保留与另一个集合的交集。
    • // 重新初始化
      set1 = new SortedSet<int> { 1, 2, 3, 4, 5 };// 交集(修改 set1)
      set1.IntersectWith(set2);
      Console.WriteLine(string.Join(", ", set1)); // 4, 5
  • 差集:ExceptWith 删除与另一个集合相交的元素。
    • // 重新初始化
      set1 = new SortedSet<int> { 1, 2, 3, 4, 5 };// 差集(set1 中有但 set2 中没有的元素)
      set1.ExceptWith(set2);
      Console.WriteLine(string.Join(", ", set1)); // 1, 2, 3
  • 对称差集:SymmetricExceptWith 两个集合中不共同拥有的元素
    • // 对称差集(两个集合中不共同拥有的元素)
      set1 = new SortedSet<int> { 1, 2, 3, 4, 5 };
      set1.SymmetricExceptWith(set2);
      Console.WriteLine(string.Join(", ", set1)); // 1, 2, 3, 6, 7, 8

集合关系判断

SortedSet<int> set1 = new SortedSet<int> { 1, 2, 3 };
SortedSet<int> set2 = new SortedSet<int> { 1, 2, 3, 4, 5 };
SortedSet<int> set3 = new SortedSet<int> { 2, 3 };
SortedSet<int> set4 = new SortedSet<int> { 6, 7 };
  • 子集判断
    • // 子集判断bool isSubset = set1.IsSubsetOf(set2);        // true
      bool isProperSubset = set1.IsProperSubsetOf(set2); // true
      bool isSuperset = set2.IsSupersetOf(set1);    // true
      bool isProperSuperset = set2.IsProperSupersetOf(set1); // true
  • 重叠判断
    • // 重叠判断
      bool overlaps = set1.Overlaps(set3);          // true(有共同元素2,3)
      bool overlaps2 = set1.Overlaps(set4);         // false(无共同元素)
  • 相等判断
    • // 相等判断
      bool areEqual = set1.SetEquals(set3);         // false

遍历和枚举

基本遍历

SortedSet<string> fruits = new SortedSet<string> { "Banana", "Apple", "Cherry" };
  • foreach 遍历(按排序顺序)
    • // foreach 遍历(按排序顺序)
      foreach (string fruit in fruits)
      {Console.WriteLine(fruit); // Apple, Banana, Cherry
      }
  • 使用枚举器
    • // 使用枚举器
      using (var enumerator = fruits.GetEnumerator())
      {while (enumerator.MoveNext()){Console.WriteLine(enumerator.Current);}
      }

反向遍历

SortedSet<int> numbers = new SortedSet<int> { 1, 3, 5, 7, 9 };// 反向遍历
foreach (int number in numbers.Reverse())
{Console.WriteLine(number); // 9, 7, 5, 3, 1
}

SortedSet 的优点和适用场景

优点

  • 自动保持元素排序,无需手动干预。
  • 确保元素唯一性,避免重复。
  • 高效的操作性能(O(log n))。
  • 支持集合操作和子集视图。

适用场景

  • 需要有序且不重复的元素集合,例如排行榜、时间线。
  • 实现优先级队列(尽管 C# 有 PriorityQueue<T>)。
  • 执行集合操作,如并集、交集等。

SortedSet 与其他集合类型的区别

  • 与 HashSet<T> 的区别
    • HashSet<T> 不保持顺序,查找时间为 O(1)。
    • SortedSet<T> 保持顺序,查找时间为 O(log n)。
  • 与 List<T> 的区别
    • List<T> 允许重复元素,不自动排序。
    • SortedSet<T> 不允许重复,自动排序。
  • 与 SortedList<TKey, TValue> 的区别
    • SortedList<TKey, TValue> 是键值对集合,键排序。
    • SortedSet<T> 是元素集合,元素本身排序。

相关文章:

  • MicroPython 开发ESP32应用教程 之 线程介绍及实例分析
  • LockSupport与Condition解析
  • 数据库大学实验二
  • 53、用例(Use Case)详解
  • Java网络编程性能优化
  • 六大常用查找算法对比分析
  • Mybatis使用update更新值为null时不生效问题解决
  • Python+AI Agent:解锁MCP Servers的智能潜力
  • (自用)Java学习-5.16(取消收藏,批量操作,修改密码,用户更新,上传头像)
  • 相机定屏问题分析四:【cameraserver 最大request buffer超标】后置视频模式预览定屏闪退至桌面
  • 自动驾驶规划控制教程——不确定环境下的决策规划
  • 函数到底有多少细节?
  • Docker+MobaXterm+x11实现容器UI界面转发本地
  • rlemasklib 安装笔记
  • algolia使用配置教程-为SSG静态站增加algolia搜索功能
  • 投影机光源三代发展史:从高压汞灯、白光 LED 到三色光源
  • 【Python】日期计算和自动化运行脚本
  • 2025年我国低空经济产业链研究与梳理
  • IEEE Journal on Selected Areas in Communications 2025年论文整理2(中英文摘要)
  • 某验4无感探针-js逆向
  • 查看网站是什么空间/seo百度快速排名
  • wordpress sqlite/杭州seo全网营销
  • 已有网站备案更换idc 多久/重庆网站建设外包
  • 大庆做网站最厉害的人/seo技术
  • 国外psd免费下载网站/竞价代运营
  • wap网站制作哪家好/陕西seo推广