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

C#のSortedDictionary.Remove()メソッドの使い方を徹底解説

C#のSortedDictionary.Remove()メソッドは、SortedDictionary<TKey, TValue>コレクションから、指定したキーに一致する要素を削除するために使用されるメソッドです。

このメソッドはbool型の値を返し、要素の削除に成功した場合はtrue、指定したキーがコレクション内に存在しない場合はfalseを返します。なお、SortedDictionaryは内部で二分探索木を使用しているため、Remove()メソッドの計算量はO(log n)となります。

構文

Remove()メソッドの構文は以下の通りです。

public bool Remove (TKey key);

パラメータのkeyには、削除したい要素のキーを指定します。

使用例1

それでは、具体的な使用例を見てみましょう。以下のコードでは、6つのキーと値のペアを持つSortedDictionaryを作成し、Remove()メソッドを使ってキー「400」の要素を削除しています。

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, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("\nKey 400 removed from SortedDictionary? = "+sortedDict.Remove(400));
      Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      SortedDictionary<int, string>.KeyCollection keyColl = sortedDict.Keys;
      Console.WriteLine("\nKeys...list");
      foreach( int i in keyColl ){
         Console.WriteLine(i);
      }
   }
}

出力結果

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

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone
Key 400 removed from SortedDictionary? = True
SortedDictionary key-value pairs...UPDATED
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 500, Value = Headphone
Key = 600, Value = Earphone
Keys...list
100
200
300
500
600

実行結果を見ると、sortedDict.Remove(400)の呼び出しがTrueを返し、キー「400」の要素(Value = Speakers)がコレクションから正常に削除されていることが確認できます。

使用例2

続いて、別の例を見てみましょう。今度は8つの要素を持つSortedDictionaryから、キー「7」の要素を削除します。

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, "Ultrabook");
      sortedDict.Add(2, "Alienware");
      sortedDict.Add(3, "Notebook");
      sortedDict.Add(4, "Connector");
      sortedDict.Add(5, "Flash Drive");
      sortedDict.Add(6, "SSD");
      sortedDict.Add(7, "HDD");
      sortedDict.Add(8, "Earphone");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("\nKey 7 removed from SortedDictionary? = "+sortedDict.Remove(7));
      Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      SortedDictionary<int, string>.KeyCollection keyColl = sortedDict.Keys;
      Console.WriteLine("\nKeys...list");
      foreach( int i in keyColl ){
         Console.WriteLine(i);
      }
   }
}

出力結果

このコードを実行すると、以下の出力が得られます。

SortedDictionary key-value pairs...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 7, Value = HDD
Key = 8, Value = Earphone
Key 7 removed from SortedDictionary? = True
SortedDictionary key-value pairs...UPDATED
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 8, Value = Earphone
Keys...list
1
2
3
4
5
6
8

まとめ

SortedDictionary.Remove()メソッドを使うことで、指定したキーを持つ要素を簡単に削除できます。ポイントは以下の通りです。

  • 引数には削除対象のキーを1つだけ指定する
  • 削除に成功するとtrue、キーが存在しない場合はfalseを返すため、例外をスローせずに安全に処理できる
  • 計算量はO(log n)で、大量のデータでも効率的に削除処理が行える

キーが存在するかどうか事前に確認する必要がなく、戻り値で成否を判定できる点が、このメソッドの大きな利点といえます。

  1. HTML DOM Select remove()メソッドの使い方を徹底解説!ドロップダウンリストから選択肢を削除する方法

    HTML DOM の select remove() メソッドは、HTML ドキュメント内のドロップダウンリスト(<select> 要素)から、指定したオプション(選択肢)を削除するためのメソッドです。ユーザーの操作に応じて動的にリストの内容を変更したい場合に非常に便利です。 構文 remove() メソッドの基本的な構文は以下のとおりです。 object.remove(index) index には、削除したいオプションのインデックス番号を指定します。インデックスは 0 から始まる点に注意してください。つまり、最初の選択肢を削除する場合は「0」、2番目の選択肢を削除する場合は「1

  2. HTML DOM Storage key()メソッドの使い方を徹底解説

    HTML DOM Storage key()メソッドとはHTML DOMのStorage key()メソッドは、ストレージオブジェクト内の指定したインデックス位置に保存されているキー名を取得するためのメソッドです。取得したいキーの番号(インデックス)は、引数としてkey()メソッドに渡します。このメソッドで利用できるストレージオブジェクトは、sessionStorageオブジェクトとlocalStorageオブジェクトの2種類です。構文Storage key()メソッドの基本構文は以下のとおりです。localStorageの場合localStorage.key(index);sessionSt