Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C# Hashtable에서 특정 키의 요소를 제거하는 방법

C#에서 Hashtable은 키와 값 쌍으로 데이터를 저장하는 대표적인 컬렉션입니다. 저장된 요소 중 특정 키를 가진 항목을 삭제하려면 Remove() 메서드를 사용하면 됩니다. 이 메서드는 매개변수로 전달된 키에 해당하는 요소를 Hashtable에서 제거합니다.

Remove() 메서드 기본 문법

public virtual void Remove(object key);

지정한 키가 Hashtable에 존재하지 않더라도 Remove() 메서드는 예외를 발생시키지 않고 그대로 통과합니다.

예제 1: 단일 키 제거하기

다음 예제는 Hashtable에 여러 개의 키와 값을 추가한 후, Remove() 메서드로 하나의 요소를 제거하는 과정을 보여줍니다.

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      Hashtable hash = new Hashtable(10);
      hash.Add("1", "A");
      hash.Add("2", "B");
      hash.Add("3", "C");
      hash.Add("4", "D");
      hash.Add("5", "E");
      hash.Add("6", "F");
      hash.Add("7", "G");
      hash.Add("8", "H");
      hash.Add("9", "I");
      hash.Add("10", "J");

      Console.WriteLine("Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash){
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }

      Console.WriteLine("Is the Hashtable having fixed size? = " + hash.IsFixedSize);
      Console.WriteLine("Count of entries in Hashtable = " + hash.Count);

      // 키 "5"인 요소 제거
      hash.Remove("5");

      Console.WriteLine("Count of entries in Hashtable (updated) = " + hash.Count);
   }
}

출력 결과

Hashtable Key and Value pairs...
10 and J
1 and A
2 and B
3 and C
4 and D
5 and E
6 and F
7 and G
8 and H
9 and I
Is the Hashtable having fixed size? = False
Count of entries in Hashtable = 10
Count of entries in Hashtable (updated) = 9

실행 결과를 보면 IsFixedSize 속성이 False이므로 Hashtable은 크기가 고정되어 있지 않습니다. 처음에는 10개의 항목이 있었지만, hash.Remove("5")를 호출한 후 항목 수가 9개로 감소한 것을 확인할 수 있습니다.

예제 2: 여러 키 한 번에 제거하기

이번에는 부서 정보를 담은 Hashtable에서 여러 키를 연속으로 제거하는 예제입니다.

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      Hashtable hash = new Hashtable(10);
      hash.Add("One", "Marketing");
      hash.Add("Two", "Operations");
      hash.Add("Three", "Finance");
      hash.Add("Four", "IT");
      hash.Add("Five", "Sales");
      hash.Add("Six", "Purchase");

      Console.WriteLine("Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash){
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }

      Console.WriteLine("Count of entries in Hashtable = " + hash.Count);

      // 여러 키 제거
      hash.Remove("Two");
      hash.Remove("Three");
      hash.Remove("Four");

      Console.WriteLine("Count of entries in Hashtable (updated) = " + hash.Count);
   }
}

출력 결과

Hashtable Key and Value pairs...
One and Marketing
Five and Sales
Three and Finance
Two and Operations
Four and IT
Six and Purchase
Count of entries in Hashtable = 6
Count of entries in Hashtable (updated) = 3

정리

  • Remove() 메서드는 전달받은 키와 일치하는 요소를 Hashtable에서 삭제합니다.
  • 존재하지 않는 키를 전달해도 예외가 발생하지 않습니다.
  • Count 속성으로 제거 후 남아 있는 항목 수를 확인할 수 있습니다.

Hashtable은 해시 코드 기반으로 데이터를 관리하기 때문에 요소를 열거할 때 입력 순서대로 출력되지 않는 점도 참고하세요. 출력 결과에서 보듯이 항목 순서는 해싱 알고리즘에 따라 달라질 수 있습니다.