C#
 Computer >> コンピューター >  >> プログラミング >> C#

【C#入門】SortedDictionary.ContainsValue()メソッドで指定した値を検索する方法

C#のSortedDictionary.ContainsValue()メソッドは、SortedDictionary<TKey, TValue>コレクション内に、指定した値を持つ要素が存在するかどうかを判定するために使用します。

指定した値がコレクション内に見つかった場合は true、見つからなかった場合は false を返します。このメソッドが検索するのは「キー」ではなく「値」である点に注意してください。キーの存在を確認したい場合は、別途 ContainsKey() メソッドを使用します。

構文

構文は以下のとおりです。

public bool ContainsValue (TValue val);

パラメータ val には、SortedDictionary<TKey, TValue> 内で検索したい値を指定します。

  • 戻り値: 指定した値が SortedDictionary 内に存在する場合は true、存在しない場合は false。

使用例1:値が存在する場合

まずは、基本的な使用例を見てみましょう。ここでは、複数の製品名を値として登録し、「Notebook」という値が含まれているかどうかを確認しています。

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Inspiron");
      sortedDict.Add(200, "Alienware");
      sortedDict.Add(300, "Projectors");
      sortedDict.Add(400, "XPS");

      Console.WriteLine("SortedDictionaryのキーと値のペア...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("SortedDictionaryのキーと値のペア数 = " + sortedDict.Count);

      sortedDict.Add(800, "Notebook");
      sortedDict.Add(10000, "Bluetooth Speaker");

      Console.WriteLine("\nSortedDictionaryのキーと値のペア...更新後");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("SortedDictionaryのキーと値のペア数(更新後) = " + sortedDict.Count);

      Console.WriteLine("値\"Notebook\"はSortedDictionaryに存在するか = " + sortedDict.ContainsValue("Notebook"));

      sortedDict.Clear();
      Console.WriteLine("\nSortedDictionaryのキーと値のペア数(クリア後) = " + sortedDict.Count);
   }
}

出力結果

上記のコードを実行すると、次のような出力が得られます。

SortedDictionaryのキーと値のペア...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
SortedDictionaryのキーと値のペア数 = 4
SortedDictionaryのキーと値のペア...更新後
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
SortedDictionaryのキーと値のペア数(更新後) = 6
値"Notebook"はSortedDictionaryに存在するか = True
SortedDictionaryのキーと値のペア数(クリア後) = 0

この例では、「Notebook」という値がコレクション内に存在するため、ContainsValue()True を返しています。

使用例2:値が存在しない場合

次に、存在しない値を検索した場合の動作を確認してみましょう。

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(1, "One");
      sortedDict.Add(2, "Two");
      sortedDict.Add(3, "Three");
      sortedDict.Add(4, "Four");
      sortedDict.Add(5, "Five");
      sortedDict.Add(6, "Six");

      Console.WriteLine("SortedDictionaryのキーと値のペア...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("SortedDictionaryのキーと値のペア数 = " + sortedDict.Count);

      sortedDict.Add(7, "Seven");
      sortedDict.Add(8, "Eight");

      Console.WriteLine("\nSortedDictionaryのキーと値のペア...更新後");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("SortedDictionaryのキーと値のペア数(更新後) = " + sortedDict.Count);

      Console.WriteLine("値\"Eleven\"はSortedDictionaryに存在するか = " + sortedDict.ContainsValue("Eleven"));

      sortedDict.Clear();
      Console.WriteLine("\nSortedDictionaryのキーと値のペア数(クリア後) = " + sortedDict.Count);
   }
}

出力結果

上記のコードを実行すると、次のような出力が得られます。

SortedDictionaryのキーと値のペア...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
SortedDictionaryのキーと値のペア数 = 6
SortedDictionaryのキーと値のペア...更新後
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Key = 7, Value = Seven
Key = 8, Value = Eight
SortedDictionaryのキーと値のペア数(更新後) = 8
値"Eleven"はSortedDictionaryに存在するか = False
SortedDictionaryのキーと値のペア数(クリア後) = 0

この例では、「Eleven」という値はコレクション内に存在しないため、ContainsValue()False を返しています。

補足:パフォーマンスに関する注意点

ContainsValue() メソッドは、内部で線形探索(計算量 O(n))によってすべての値を順番に比較します。そのため、要素数が多いコレクションに対して頻繁に呼び出すと、パフォーマンスに影響が出る可能性があります。一方、キーによる検索である ContainsKey() は二分探索木を利用しており高速です。用途に応じて適切なメソッドを選択しましょう。

  1. C#のDictionary.ContainsValue()メソッドの使い方を解説

    C#のDictionary.ContainsValue()メソッドは、Dictionary<TKey, TValue>コレクション内に指定した値が存在するかどうかを確認するためのメソッドです。値が見つかった場合は true を返し、見つからない場合は false を返します。構文public bool ContainsValue (TValue val);パラメータ val には、検索対象となる値を指定します。ContainsValue()メソッドの特徴線形探索(O(n))で値を検索するため、要素数が多い場合は処理に時間がかかることがあります。キーの検索に使用する Contains

  2. C#のConvert.ToDoubleメソッドで値をdouble型に変換する方法

    C#のConvert.ToDoubleメソッドとはC#では、指定した値を倍精度浮動小数点数(double型)に変換したい場合、Convert.ToDouble()メソッドを使用します。このメソッドは、数値型だけでなく文字列などさまざまな型の値をdouble型に変換できる便利なメソッドです。ここでは、long型の値をdouble型に変換する例を紹介します。変換元のlong型の値まず、変換対象となるlong型の値を用意します。long[] val = { 340, -200};double型への変換Convert.ToDouble()メソッドを使って、これらの値をdouble型に変換します。dou