C# SortedSet 示例
介绍
在任何编程语言中,集合都扮演着非常重要的角色。很多时候我们需要 Sorted 集合,所以我觉得在这篇文章中讨论 SortedSet 集合。本文可供初学者、中级和专业人士使用。
我们将覆盖,
- 我们应该在哪里使用它?
- 什么是 SortedSet 集合?
- 如何在SortedSet集合中添加元素?
- 如何从有序集合集合中删除元素?
- Remove 方法
- Clear 方法
- RemoveWhere
- SortedSet 集合中可用的方法
- Count
- UnionWith
- SymmetricExceptWith
- ExceptWith
- Overlaps
- IntersectWith
- Min, max
- SetEquals
- GetViewBetween
让我们开始,
我们应该在哪里使用 SortedSet?
假设您需要存储需要按排序顺序的唯一元素,那么 SortedSet 是您的正确选择。默认情况下,它将按升序排序。
什么是 SortedSet 集合?
SortedSet 是按排序顺序的对象的通用集合。SortedSet 集合(如 HashSet 集合)中不允许出现重复元素。
SortedSet 是 System.Collections.Generic 中定义的类。
它是动态集合意味着它可以在您添加元素时增长,并在您从集合中删除元素时收缩。
SortedSet<T> 类的语法
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlySet<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
以下接口在 SortedSet<T> 类中实现。
- ICollection<T>
- IEnumerable<T>
- IEnumerable
- IReadOnlyCollection<T>
- ISet<T>
- ICollection
- IDeserializationCallback
- ISerializable
如何创建 SortedSet 集合?
在这里,我将创建一个空的 SortedSet 集合。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
}
}
}
如何在SortedSet集合中添加元素?
我们将使用 Add 方法向集合中添加元素。在下面的示例中,我们将向集合中添加城市,然后在屏幕上打印。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
输出
SortedSet 集合中的包含方法
包含方法用于查看元素是否存在于集合中。让我们看看下面的例子。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
if(sortedSetDemo.Contains("Vadodara"))
{
Console.WriteLine("Vadodara is present in the collection");
}
else
{
Console.WriteLine("Vadodara is not present in the collection");
}
Console.ReadLine();
}
}
}
输出
如何从集合中删除元素?
我们可以根据需要使用 Remove、Clear 和 RemoveWhere 方法从 Collection 中删除元素。
Remove
此方法用于从集合中删除特定项目。看下面的例子,
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
sortedSetDemo.Remove("Surat");
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
输出
在上面的代码中,从集合中删除了“Surat”。
Clear()
此方法用于从集合中删除所有项目。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
sortedSetDemo.Clear();
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.WriteLine("Clear all elements from collection");
Console.ReadLine();
}
}
}
输出
RemoveWhere
此方法用于删除与方法中定义的条件匹配的所有元素。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
Console.WriteLine(collection1.Overlaps(collection2));
Console.ReadLine();
}
}
}
输出
IntersectWith
此方法从集合中返回常见项目。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
collection1.IntersectWith(collection2);
foreach (var item in collection1)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
输出
SymmetricExceptWith
此方法返回两个集合中都不存在的所有元素。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
collection1.SymmetricExceptWith(collection2);
foreach (var item in collection1)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
输出
Min, max
此方法从集合中返回 Min 和 Max 值。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
Console.WriteLine(collection2.Min);
Console.Write(collection2.Max);
Console.ReadLine();
}
}
}
输出
SetEquals
如果两个集合具有相同的项目,则此方法返回 true。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Vadodara");
collection1.Add("Dabhoi");
collection1.Add("Mathura");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
Console.WriteLine(collection1.SetEquals(collection2));
Console.ReadLine();
}
}
}
输出
GetViewBetween
此方法从给定范围之间的集合中返回项目。
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<int> collection1 = new SortedSet<int>();
collection1.Add(9);
collection1.Add(5);
collection1.Add(7);
collection1.Add(4);
collection1.Add(6);
foreach (var item in collection1.GetViewBetween(4,8))
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
输出
希望你喜欢这篇文章并发现它很有用。感谢您阅读这篇文章。
常见问题FAQ
- 程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
- 请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!