C# SortedSet 示例

作者 : 慕源网 本文共8991个字,预计阅读时间需要23分钟 发布时间: 2021-10-31 共453人阅读

介绍

在任何编程语言中,集合都扮演着非常重要的角色。很多时候我们需要 Sorted 集合,所以我觉得在这篇文章中讨论 SortedSet 集合。本文可供初学者、中级和专业人士使用。 

我们将覆盖,

  1. 我们应该在哪里使用它?
  2. 什么是 SortedSet 集合?
  3. 如何在SortedSet集合中添加元素?
  4. 如何从有序集合集合中删除元素?
    • Remove 方法
    • Clear 方法
    • RemoveWhere
  5. 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> 类中实现。

  1. ICollection<T>
  2. IEnumerable<T> 
  3. IEnumerable 
  4. IReadOnlyCollection<T> 
  5. ISet<T>
  6. ICollection
  7. IDeserializationCallback
  8. 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> sortedSetDemo = new SortedSet<string>();
            sortedSetDemo.Add("Mumbai");
            sortedSetDemo.Add("Surat");
            sortedSetDemo.Add("Vadodara");
            sortedSetDemo.Add("Dabhoi");
            sortedSetDemo.Add("Pune");
            sortedSetDemo.Add("Mathura");

            sortedSetDemo.RemoveWhere(myFunc);

            foreach (var item in sortedSetDemo)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
        private static bool myFunc(string strcity)
        {
            if(strcity.StartsWith('M'))
            {
                return true;
            }
            return false;

        }
    }
}

输出

我们已经使用 RemoveWhere 方法从集合中删除了所有以“M”开头的城市。

SortedSet 集合中可用的方法

sortedSet 集合中提供了许多方法来执行各种操作。我们将在这里讨论一些重要的方法。

CopyList

在下面的示例中,我们已将列表复制到 sortedset。

using System;
using System.Collections.Generic;

namespace SortedSetDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lstString = new List<string>()
            {
                "Mumbai",
                "Surat",
                "Vadodara",
                "Dabhoi",
                "Pune",
                "Mathura"
            };

            SortedSet<string> sortedSetDemo = new SortedSet<string>(lstString);

            foreach (var item in sortedSetDemo)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

输出

Count, Clear方法

count 方法用于获取 SortedSet 中可用的项目数。clear 方法用于清除 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");
            sortedSetDemo.Add("Mathura");

            Console.WriteLine(sortedSetDemo.Count);
            sortedSetDemo.Clear();

            foreach (var item in sortedSetDemo)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

输出

UnionWith

此方法返回两个集合的并集。请看下面的例子,

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");

            SortedSet<string> collection2 = new SortedSet<string>();
            collection2.Add("Vadodara");
            collection2.Add("Dabhoi");
            collection2.Add("Pune");
            collection2.Add("Mathura");

            collection1.UnionWith(collection2);
            foreach (var item in collection1)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

输出

ExceptWith

这是从集合中删除匹配项。

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.ExceptWith(collection2);
            foreach (var item in collection1)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

输出

OverLaps

此方法比较 2 个集合,即使找到单个匹配项也返回 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("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();
        }
    }
}

输出

希望你喜欢这篇文章并发现它很有用。感谢您阅读这篇文章。


慕源网 » C# SortedSet 示例

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录