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>
- 屬性
- 實作
範例
下列範例示範使用採用 IComparer<T> 做為參數的建構函式所建立的 SortedSet<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 |
取得 IComparer<T> 對象,這個物件用來排序 SortedSet<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> 實例所需的數據。 |