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

C# Hashtable에서 특정 키가 존재하는지 확인하는 방법

C# Hashtable에서 특정 키가 있는지 확인하는 방법

C#의 Hashtable 컬렉션에 특정 키가 존재하는지 확인하려면 ContainsKey() 메서드를 사용합니다. 이 메서드는 지정한 키가 Hashtable에 포함되어 있으면 true, 없으면 false를 반환합니다.

Hashtable은 키(Key)와 값(Value)을 한 쌍으로 저장하는 해시 기반 컬렉션입니다. 키를 통해 값에 빠르게 접근할 수 있으며, ContainsKey() 메서드를 활용하면 원하는 키의 존재 여부를 손쉽게 검사할 수 있습니다. 아래 예제를 통해 실제 사용 방법을 살펴보겠습니다.

예제 1

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      Hashtable hash = new Hashtable();
      hash.Add("One", "Katie");
      hash.Add("Two", "John");
      hash.Add("Three", "Barry");
      hash.Add("Four", "Mark");
      hash.Add("Five", "Harry");
      hash.Add("Six", "Nathan");
      hash.Add("Seven", "Tom");
      hash.Add("Eight", "Andy");
      hash.Add("Nine", "Illeana");
      hash.Add("Ten", "Tim");

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

      Console.WriteLine("Is Hashtable having fixed size? = " + hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = " + hash.IsReadOnly);
      Console.WriteLine("The Hashtable consists of the key? = " + hash.ContainsKey("Seven"));
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

Hashtable Key and Value pairs...
One and Katie
Ten and Tim
Five and Harry
Three and Barry
Seven and Tom
Two and John
Four and Mark
Eight and Andy
Nine and Illeana
Six and Nathan
Is Hashtable having fixed size? = False
If Hashtable read-only? = False
The Hashtable consists of the key? = True

실행 결과에서 볼 수 있듯이, Hashtable에는 키 "Seven"(값 "Tom")이 존재하므로 ContainsKey("Seven") 호출 시 true가 반환됩니다. 또한 IsFixedSize와 IsReadOnly 속성이 모두 False로 출력되는 것도 함께 확인할 수 있습니다.

예제 2

이번에는 숫자 문자열을 키로 사용하는 또 다른 예제를 살펴보겠습니다.

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      Hashtable hash = new Hashtable();
      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 Hashtable having fixed size? = " + hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = " + hash.IsReadOnly);
      Console.WriteLine("The Hashtable consists of the key? = " + hash.ContainsKey("5"));
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

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 Hashtable having fixed size? = False
If Hashtable read-only? = False
The Hashtable consists of the key? = True

정리

Hashtable에 특정 키가 있는지 확인할 때는 ContainsKey() 메서드를 사용하는 것이 가장 간단하고 확실한 방법입니다. ContainsKey()는 평균적으로 O(1)의 시간 복잡도를 가지므로 대용량 데이터에서도 매우 빠른 검색이 가능합니다. 반대로 특정 값(Value)의 존재 여부를 확인해야 한다면 ContainsValue() 메서드를 사용하면 됩니다.