SortedDictionary<TKey,TValue> クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
キーに基づいて並べ替えられた、キーと値のペアのコレクションを表します。
generic <typename TKey, typename TValue>
public ref class SortedDictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyDictionary<TKey, TValue>, System::Collections::IDictionary
generic <typename TKey, typename TValue>
public ref class SortedDictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::IDictionary
public class SortedDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
[System.Serializable]
public class SortedDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionary
[System.Serializable]
public class SortedDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
public class SortedDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionary
type SortedDictionary<'Key, 'Value> = class
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface IDictionary<'Key, 'Value>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
interface IReadOnlyDictionary<'Key, 'Value>
interface ICollection
interface IDictionary
[<System.Serializable>]
type SortedDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IDictionary
interface ICollection
interface IEnumerable
[<System.Serializable>]
type SortedDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface IDictionary
interface ICollection
interface IReadOnlyDictionary<'Key, 'Value>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
type SortedDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IDictionary
interface ICollection
interface IEnumerable
[<System.Serializable>]
type SortedDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface IDictionary
interface IReadOnlyDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
Public Class SortedDictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue)), IReadOnlyCollection(Of KeyValuePair(Of TKey, TValue)), IReadOnlyDictionary(Of TKey, TValue)
Public Class SortedDictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue))
型パラメーター
- TKey
ディクショナリ内のキーの型。
- TValue
ディクショナリ内の値の型。
- 継承
-
SortedDictionary<TKey,TValue>
- 属性
- 実装
-
ICollection<KeyValuePair<TKey,TValue>> IDictionary<TKey,TValue> IEnumerable<KeyValuePair<TKey,TValue>> IEnumerable<T> IReadOnlyCollection<KeyValuePair<TKey,TValue>> IReadOnlyDictionary<TKey,TValue> ICollection IDictionary IEnumerable
例
次のコード例では、文字列キーを SortedDictionary<TKey,TValue> 含む空の文字列を作成し、 メソッドを Add 使用していくつかの要素を追加します。 この例では、重複するキーを Add 追加しようとしたときに メソッドが を ArgumentException スローすることを示します。
この例では、 Item[] プロパティ (C# のインデクサー) を使用して値を取得し、要求されたキーが存在しない場合に がスローされることを示 KeyNotFoundException し、キーに関連付けられている値を置き換えることができることを示しています。
例を使用する方法を示します、TryGetValueメソッド値を取得する場合は、プログラムは多くの場合、ディクショナリに含まれていないキーの値を試行する必要があり、使用する方法を示しますより効率的な方法として、 ContainsKey を呼び出す前に、キーが存在するかどうかをテストする方法を Add メソッドです。
この例では、ディクショナリ内のキーと値を列挙する方法と、 プロパティと プロパティValuesを使用してKeysキーと値を列挙する方法を示します。
最後に、 メソッドの例を Remove 示します。
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith =
new SortedDictionary<string, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";
// The indexer throws an exception if the requested key is
// not in the dictionary.
try
{
Console.WriteLine("For key = \"tif\", value = {0}.",
openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
Console.WriteLine("Key = \"tif\" is not found.");
}
// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}",
openWith["ht"]);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
// To get the values alone, use the Values property.
SortedDictionary<string, string>.ValueCollection valueColl =
openWith.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach( string s in valueColl )
{
Console.WriteLine("Value = {0}", s);
}
// To get the keys alone, use the Keys property.
SortedDictionary<string, string>.KeyCollection keyColl =
openWith.Keys;
// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )
{
Console.WriteLine("Key = {0}", s);
}
// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}
}
}
/* This code example produces the following output:
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe
Key = rtf, Value = winword.exe
Key = txt, Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = hypertrm.exe
Value = winword.exe
Value = notepad.exe
Key = bmp
Key = dib
Key = doc
Key = ht
Key = rtf
Key = txt
Remove("doc")
Key "doc" is not found.
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted dictionary of strings, with string
' keys.
Dim openWith As New SortedDictionary(Of String, String)
' Add some elements to the dictionary. There are no
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' The Add method throws an exception if the new key is
' already in the dictionary.
Try
openWith.Add("txt", "winword.exe")
Catch
Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
' The Item property is the default property, so you
' can omit its name when accessing elements.
Console.WriteLine("For key = ""rtf"", value = {0}.", _
openWith("rtf"))
' The default Item property can be used to change the value
' associated with a key.
openWith("rtf") = "winword.exe"
Console.WriteLine("For key = ""rtf"", value = {0}.", _
openWith("rtf"))
' If a key does not exist, setting the default Item property
' for that key adds a new key/value pair.
openWith("doc") = "winword.exe"
' The default Item property throws an exception if the requested
' key is not in the dictionary.
Try
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
Catch
Console.WriteLine("Key = ""tif"" is not found.")
End Try
' When a program often has to try keys that turn out not to
' be in the dictionary, TryGetValue can be a more efficient
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
Console.WriteLine("Key = ""tif"" is not found.")
End If
' ContainsKey can be used to test keys before inserting
' them.
If Not openWith.ContainsKey("ht") Then
openWith.Add("ht", "hypertrm.exe")
Console.WriteLine("Value added for key = ""ht"": {0}", _
openWith("ht"))
End If
' When you use foreach to enumerate dictionary elements,
' the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
' To get the values alone, use the Values property.
Dim valueColl _
As SortedDictionary(Of String, String).ValueCollection = _
openWith.Values
' The elements of the ValueCollection are strongly typed
' with the type that was specified for dictionary values.
Console.WriteLine()
For Each s As String In valueColl
Console.WriteLine("Value = {0}", s)
Next s
' To get the keys alone, use the Keys property.
Dim keyColl _
As SortedDictionary(Of String, String).KeyCollection = _
openWith.Keys
' The elements of the KeyCollection are strongly typed
' with the type that was specified for dictionary keys.
Console.WriteLine()
For Each s As String In keyColl
Console.WriteLine("Key = {0}", s)
Next s
' Use the Remove method to remove a key/value pair.
Console.WriteLine(vbLf + "Remove(""doc"")")
openWith.Remove("doc")
If Not openWith.ContainsKey("doc") Then
Console.WriteLine("Key ""doc"" is not found.")
End If
End Sub
End Class
' This code example produces the following output:
'
'An element with Key = "txt" already exists.
'For key = "rtf", value = wordpad.exe.
'For key = "rtf", value = winword.exe.
'Key = "tif" is not found.
'Key = "tif" is not found.
'Value added for key = "ht": hypertrm.exe
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = doc, Value = winword.exe
'Key = ht, Value = hypertrm.exe
'Key = rtf, Value = winword.exe
'Key = txt, Value = notepad.exe
'
'Value = paint.exe
'Value = paint.exe
'Value = winword.exe
'Value = hypertrm.exe
'Value = winword.exe
'Value = notepad.exe
'
'Key = bmp
'Key = dib
'Key = doc
'Key = ht
'Key = rtf
'Key = txt
'
'Remove("doc")
'Key "doc" is not found.
'
注釈
ジェネリック クラスは SortedDictionary<TKey,TValue> 、O(log n) 取得を含むバイナリ検索ツリーです。ここで、n はディクショナリ内の要素の数です。 この点では、ジェネリック クラスに SortedList<TKey,TValue> 似ています。 2 つのクラスには同様のオブジェクト モデルがあり、どちらも O(log n) の取得があります。 2 つのクラスが異なるのは、メモリの使用と挿入と削除の速度です。
SortedList<TKey,TValue> より少ないメモリを SortedDictionary<TKey,TValue>使用します。
SortedDictionary<TKey,TValue> には、並べ替えされていないデータに対する挿入操作と削除操作が高速です。 の場合は O(n) ではなく、O(log n) SortedList<TKey,TValue>です。
一覧が並べ替えられたデータから一度にすべて設定される場合は、 SortedList<TKey,TValue> よりも SortedDictionary<TKey,TValue>高速です。
各キーと値のペアは、構造体として KeyValuePair<TKey,TValue> 、または DictionaryEntry 非ジェネリック IDictionary インターフェイスを介して として取得できます。
キーは、 内 SortedDictionary<TKey,TValue>のキーとして使用されている限り変更できない必要があります。 内のすべてのキーは SortedDictionary<TKey,TValue> 一意である必要があります。 キーを に null
することはできませんが、値の型が参照型 TValue
の場合は、 を指定できます。
SortedDictionary<TKey,TValue> キー比較を実行するには、比較子の実装が必要です。 パラメーターを受け取るコンストラクターを IComparer<T> 使用してジェネリック インターフェイスの実装を comparer
指定できます。実装を指定しない場合は、既定のジェネリック 比較子 Comparer<T>.Default が使用されます。 type TKey
がジェネリック インターフェイスを実装する System.IComparable<T> 場合、既定の比較子はその実装を使用します。
C# 言語の ステートメント (for each
Visual Basic では C++) はforeach
、For Each
コレクション内の要素の型のオブジェクトを返します。 の SortedDictionary<TKey,TValue> 各要素はキーと値のペアであるため、要素の型はキーの型または値の型ではありません。 代わりに、要素の型は です KeyValuePair<TKey,TValue>。 次のコードは、C#、C++、Visual Basic の構文を示しています。
for each(KeyValuePair<String^, String^> kvp in myDictionary)
{
Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of String, String) In myDictionary
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp
ステートメントは foreach
列挙子を囲むラッパーであり、コレクションからの読み取りのみを許可し、コレクションへの書き込みは許可しません。
コンストラクター
SortedDictionary<TKey,TValue>() |
空で、キーの型の既定の SortedDictionary<TKey,TValue> 実装を使用する、IComparer<T> クラスの新しいインスタンスを初期化します。 |
SortedDictionary<TKey,TValue>(IComparer<TKey>) |
空で、指定した SortedDictionary<TKey,TValue> を使用してキーを比較する、IComparer<T> クラスの新しいインスタンスを初期化します。 |
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>) |
指定した SortedDictionary<TKey,TValue> から要素をコピーして格納し、キーの型の既定の IDictionary<TKey,TValue> 実装を使用する、IComparer<T> クラスの新しいインスタンスを初期化します。 |
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
指定した SortedDictionary<TKey,TValue> から要素をコピーして格納し、指定した IDictionary<TKey,TValue> 実装を使用してキーを比較する、IComparer<T> クラスの新しいインスタンスを初期化します。 |
プロパティ
Comparer |
IComparer<T> の要素の順序付けに使用する SortedDictionary<TKey,TValue> を取得します。 |
Count |
SortedDictionary<TKey,TValue> に格納されているキー/値ペアの数を取得します。 |
Item[TKey] |
指定されたキーに関連付けられている値を取得または設定します。 |
Keys |
SortedDictionary<TKey,TValue> 内のキーを格納しているコレクションを取得します。 |
Values |
SortedDictionary<TKey,TValue> 内の値を格納しているコレクションを取得します。 |
メソッド
Add(TKey, TValue) |
指定したキーおよび値を持つ要素を SortedDictionary<TKey,TValue> に追加します。 |
Clear() |
SortedDictionary<TKey,TValue> からすべての要素を削除します。 |
ContainsKey(TKey) |
指定したキーの要素が SortedDictionary<TKey,TValue> に格納されているかどうかを確認します。 |
ContainsValue(TValue) |
指定した値の要素が SortedDictionary<TKey,TValue> に格納されているかどうかを確認します。 |
CopyTo(KeyValuePair<TKey,TValue>[], Int32) |
指定したインデックスを開始位置として、指定した SortedDictionary<TKey,TValue> 構造体の配列に KeyValuePair<TKey,TValue> 要素をコピーします。 |
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
GetEnumerator() |
SortedDictionary<TKey,TValue> を反復処理する列挙子を返します。 |
GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
Remove(TKey) |
指定したキーを持つ要素を SortedDictionary<TKey,TValue> から削除します。 |
ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
TryGetValue(TKey, TValue) |
指定したキーに関連付けられている値を取得します。 |
明示的なインターフェイスの実装
拡張メソッド
適用対象
スレッド セーフ
パブリック静的 (Visual Basic ではShared
) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。
は SortedDictionary<TKey,TValue> 、コレクションが変更されていない限り、複数のリーダーを同時にサポートできます。 それでも、コレクションを列挙することは本質的にスレッド セーフなプロシージャではありません。 列挙処理でスレッド セーフを確保するには、列挙処理が終わるまでコレクションをロックできます。 コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。