SortedSet<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示按排序顺序维护的对象集合。
generic <typename T>
public ref class SortedSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::ISet<T>, System::Collections::ICollection
generic <typename T>
public ref class SortedSet : 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
generic <typename T>
public ref class SortedSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::ISet<T>, System::Collections::ICollection, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class SortedSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::ISet<T>, System::Collections::ICollection, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class SortedSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::ISet<T>, System::Collections::ICollection
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection
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
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection
type SortedSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ISet<'T>
interface ICollection
type SortedSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ISet<'T>
interface IReadOnlySet<'T>
interface ICollection
interface IDeserializationCallback
interface ISerializable
type SortedSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ISet<'T>
interface ICollection
interface IDeserializationCallback
interface ISerializable
[<System.Serializable>]
type SortedSet<'T> = class
interface ISet<'T>
interface ICollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
[<System.Serializable>]
type SortedSet<'T> = class
interface ISet<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface ICollection
interface ISerializable
interface IDeserializationCallback
interface IReadOnlyCollection<'T>
type SortedSet<'T> = class
interface ISet<'T>
interface ICollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
Public Class SortedSet(Of T)
Implements ICollection, ICollection(Of T), IEnumerable(Of T), IReadOnlyCollection(Of T), ISet(Of T)
Public Class SortedSet(Of T)
Implements ICollection, ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), IReadOnlyCollection(Of T), IReadOnlySet(Of T), ISerializable, ISet(Of T)
Public Class SortedSet(Of T)
Implements ICollection, ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), IReadOnlyCollection(Of T), ISerializable, ISet(Of T)
Public Class SortedSet(Of T)
Implements ICollection, ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), ISerializable, ISet(Of T)
Public Class SortedSet(Of T)
Implements ICollection, ICollection(Of T), IEnumerable(Of T), ISet(Of T)
类型参数
- T
集中元素的类型。
- 继承
-
SortedSet<T>
- 属性
- 实现
示例
以下示例演示一个 SortedSet<T> 类,该类是使用采用 IComparer<T> 作为参数的构造函数创建的。 此比较器(ByFileExtension
)用于按文件扩展名对文件名列表进行排序。
此示例演示如何创建一组已排序的媒体文件名、删除不需要的元素、查看一系列元素,以及将集与另一个排序集进行比较。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(string[] args)
{
try
{
// Get a list of the files to use for the sorted set.
IEnumerable<string> files1 =
Directory.EnumerateFiles(@"\\archives\2007\media",
"*", SearchOption.AllDirectories);
// Create a sorted set using the ByFileExtension comparer.
var mediaFiles1 = new SortedSet<string>(new ByFileExtension());
// Note that there is a SortedSet constructor that takes an IEnumerable,
// but to remove the path information they must be added individually.
foreach (string f in files1)
{
mediaFiles1.Add(f.Substring(f.LastIndexOf(@"\") + 1));
}
// Remove elements that have non-media extensions.
// See the 'IsDoc' method.
Console.WriteLine("Remove docs from the set...");
Console.WriteLine($"\tCount before: {mediaFiles1.Count}");
mediaFiles1.RemoveWhere(IsDoc);
Console.WriteLine($"\tCount after: {mediaFiles1.Count}");
Console.WriteLine();
// List all the avi files.
SortedSet<string> aviFiles = mediaFiles1.GetViewBetween("avi", "avj");
Console.WriteLine("AVI files:");
foreach (string avi in aviFiles)
{
Console.WriteLine($"\t{avi}");
}
Console.WriteLine();
// Create another sorted set.
IEnumerable<string> files2 =
Directory.EnumerateFiles(@"\\archives\2008\media",
"*", SearchOption.AllDirectories);
var mediaFiles2 = new SortedSet<string>(new ByFileExtension());
foreach (string f in files2)
{
mediaFiles2.Add(f.Substring(f.LastIndexOf(@"\") + 1));
}
// Remove elements in mediaFiles1 that are also in mediaFiles2.
Console.WriteLine("Remove duplicates (of mediaFiles2) from the set...");
Console.WriteLine($"\tCount before: {mediaFiles1.Count}");
mediaFiles1.ExceptWith(mediaFiles2);
Console.WriteLine($"\tCount after: {mediaFiles1.Count}");
Console.WriteLine();
Console.WriteLine("List of mediaFiles1:");
foreach (string f in mediaFiles1)
{
Console.WriteLine($"\t{f}");
}
// Create a set of the sets.
IEqualityComparer<SortedSet<string>> comparer =
SortedSet<string>.CreateSetComparer();
var allMedia = new HashSet<SortedSet<string>>(comparer);
allMedia.Add(mediaFiles1);
allMedia.Add(mediaFiles2);
}
catch(IOException ioEx)
{
Console.WriteLine(ioEx.Message);
}
catch (UnauthorizedAccessException AuthEx)
{
Console.WriteLine(AuthEx.Message);
}
}
// Defines a predicate delegate to use
// for the SortedSet.RemoveWhere method.
private static bool IsDoc(string s)
{
s = s.ToLower();
return (s.EndsWith(".txt") ||
s.EndsWith(".xls") ||
s.EndsWith(".xlsx") ||
s.EndsWith(".pdf") ||
s.EndsWith(".doc") ||
s.EndsWith(".docx"));
}
}
// Defines a comparer to create a sorted set
// that is sorted by the file extensions.
public class ByFileExtension : IComparer<string>
{
string xExt, yExt;
CaseInsensitiveComparer caseiComp = new CaseInsensitiveComparer();
public int Compare(string x, string y)
{
// Parse the extension from the file name.
xExt = x.Substring(x.LastIndexOf(".") + 1);
yExt = y.Substring(y.LastIndexOf(".") + 1);
// Compare the file extensions.
int vExt = caseiComp.Compare(xExt, yExt);
if (vExt != 0)
{
return vExt;
}
else
{
// The extension is the same,
// so compare the filenames.
return caseiComp.Compare(x, y);
}
}
}
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Module Module1
Sub Main()
Try
' Get a list of the files to use for the sorted set.
Dim files1 As IEnumerable = _
Directory.EnumerateFiles("\\archives\2007\media", "*", _
SearchOption.AllDirectories)
' Create a sorted set using the ByFileExtension comparer.
Dim mediaFiles1 As New SortedSet(Of String)(New ByFileExtension)
' Note that there is a SortedSet constructor that takes an IEnumerable,
' but to remove the path information they must be added individually.
For Each f As String In files1
mediaFiles1.Add(f.Substring((f.LastIndexOf("\") + 1)))
Next
' Remove elements that have non-media extensions. See the 'IsDoc' method.
Console.WriteLine("Remove docs from the set...")
Console.WriteLine($"{vbTab}Count before: {mediaFiles1.Count}")
mediaFiles1.RemoveWhere(AddressOf IsDoc)
Console.WriteLine($"{vbTab}Count after: {mediaFiles1.Count}")
Console.WriteLine()
' List all the avi files.
Dim aviFiles As SortedSet(Of String) = mediaFiles1.GetViewBetween("avi", "avj")
Console.WriteLine("AVI files:")
For Each avi As String In aviFiles
Console.WriteLine($"{vbTab}{avi}")
Next
Console.WriteLine()
' Create another sorted set.
Dim files2 As IEnumerable = _
Directory.EnumerateFiles("\\archives\2008\media", "*", _
SearchOption.AllDirectories)
Dim mediaFiles2 As New SortedSet(Of String)(New ByFileExtension)
For Each f As String In files2
mediaFiles2.Add(f.Substring((f.LastIndexOf("\") + 1)))
Next
' Remove elements in mediaFiles1 that are also in mediaFiles2.
Console.WriteLine("Remove duplicates (of mediaFiles2) from the set...")
Console.WriteLine($"{vbTab}Count before: {mediaFiles1.Count}")
mediaFiles1.ExceptWith(mediaFiles2)
Console.WriteLine($"{vbTab}Count after: {mediaFiles1.Count}")
Console.WriteLine()
Console.WriteLine("List of mediaFiles1:")
For Each f As String In mediaFiles1
Console.WriteLine($"{vbTab}{f}")
Next
' Create a set of the sets.
Dim comparer As IEqualityComparer(Of SortedSet(Of String)) = _
SortedSet(Of String).CreateSetComparer()
Dim allMedia As New HashSet(Of SortedSet(Of String))(comparer)
allMedia.Add(mediaFiles1)
allMedia.Add(mediaFiles2)
Catch ioEx As IOException
Console.WriteLine(ioEx.Message)
Catch AuthEx As UnauthorizedAccessException
Console.WriteLine(AuthEx.Message)
End Try
End Sub
' Defines a predicate delegate to use
' for the SortedSet.RemoveWhere method.
Private Function IsDoc(s As String) As Boolean
s = s.ToLower()
Return s.EndsWith(".txt") OrElse
s.EndsWith(".doc") OrElse
s.EndsWith(".xls") OrElse
s.EndsWith(".xlsx") OrElse
s.EndsWith(".pdf") OrElse
s.EndsWith(".doc") OrElse
s.EndsWith(".docx")
End Function
' Defines a comparer to create a sorted set
' that is sorted by the file extensions.
Public Class ByFileExtension
Implements IComparer(Of String)
Dim xExt, yExt As String
Dim caseiComp As CaseInsensitiveComparer = _
New CaseInsensitiveComparer
Public Function Compare(x As String, y As String) _
As Integer Implements IComparer(Of String).Compare
' Parse the extension from the file name.
xExt = x.Substring(x.LastIndexOf(".") + 1)
yExt = y.Substring(y.LastIndexOf(".") + 1)
' Compare the file extensions.
Dim vExt As Integer = caseiComp.Compare(xExt, yExt)
If vExt <> 0 Then
Return vExt
Else
' The extension is the same,
' so compare the filenames.
Return caseiComp.Compare(x, y)
End If
End Function
End Class
End Module
注解
SortedSet<T> 对象维护排序顺序,不会影响性能,因为元素入和删除。 不允许重复元素。 不支持更改现有项的排序值,并可能导致意外行为。
有关线程安全替代 SortedSet<T>,请参阅 ImmutableSortedSet<T>
构造函数
SortedSet<T>() |
初始化 SortedSet<T> 类的新实例。 |
SortedSet<T>(IComparer<T>) |
初始化使用指定比较器 SortedSet<T> 类的新实例。 |
SortedSet<T>(IEnumerable<T>) |
初始化 SortedSet<T> 类的新实例,该实例包含从指定可枚举集合复制的元素。 |
SortedSet<T>(IEnumerable<T>, IComparer<T>) |
初始化 SortedSet<T> 类的新实例,该实例包含从指定可枚举集合复制的元素,并使用指定的比较器。 |
SortedSet<T>(SerializationInfo, StreamingContext) |
已过时.
初始化包含序列化数据的 SortedSet<T> 类的新实例。 |
属性
Comparer |
获取用于对 SortedSet<T>中的值进行排序的 IComparer<T> 对象。 |
Count |
获取 SortedSet<T>中的元素数。 |
Max |
获取由比较器定义的 SortedSet<T>中的最大值。 |
Min |
获取由比较器定义的 SortedSet<T>中的最小值。 |
方法
显式接口实现
ICollection.CopyTo(Array, Int32) |
从指定的数组索引开始,将完整的 SortedSet<T> 复制到兼容的一维数组。 |
ICollection.IsSynchronized |
获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。 |
ICollection.SyncRoot |
获取可用于同步对 ICollection的访问的对象。 |
ICollection<T>.Add(T) |
将项添加到 ICollection<T> 对象。 |
ICollection<T>.IsReadOnly |
获取一个值,该值指示 ICollection 是否为只读。 |
IDeserializationCallback.OnDeserialization(Object) |
实现 IDeserializationCallback 接口,并在反序列化完成后引发反序列化事件。 |
IEnumerable.GetEnumerator() |
返回循环访问集合的枚举器。 |
IEnumerable<T>.GetEnumerator() |
返回循环访问集合的枚举器。 |
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
实现 ISerializable 接口,并返回序列化 SortedSet<T> 实例所需的数据。 |