C#의 SortedList는 키(key)를 기준으로 자동 정렬되는 키-값(key-value) 쌍 컬렉션입니다. 이 컬렉션에서 특정 키나 값이 존재하는지 확인하려면 ContainsKey()와 ContainsValue() 메서드를 사용하면 됩니다. 아래 예제를 통해 실제 검색 방법과 출력 결과를 살펴보겠습니다.
SortedList 검색에 활용되는 주요 멤버
- ContainsKey(object key) – 지정한 키가 SortedList에 존재하는지 확인하여 true 또는 false를 반환합니다.
- ContainsValue(object value) – 지정한 값이 SortedList에 존재하는지 확인합니다.
- IsFixedSize – SortedList가 고정 크기인지 여부를 나타냅니다.
- Count – SortedList에 포함된 키-값 쌍의 개수를 반환합니다.
- GetEnumerator() – 컬렉션을 순회할 수 있는 IDictionaryEnumerator를 반환합니다.
예제 1: ContainsValue와 ContainsKey로 검색하기
다음 예제에서는 문자열 키와 값을 가진 SortedList를 만든 뒤, 특정 값과 키의 존재 여부를 확인합니다.
using System;
using System.Collections;
public class Demo {
public static void Main() {
SortedList list = new SortedList();
list.Add("1", "One");
list.Add("2", "Two");
list.Add("3", "Three");
list.Add("4", "Four");
list.Add("5", "Five");
list.Add("6", "Six");
list.Add("7", "Seven");
list.Add("8", "Eight");
Console.WriteLine("Key and Value of SortedList....");
foreach(DictionaryEntry k in list)
Console.WriteLine("Key: {0}, Value: {1}", k.Key, k.Value);
Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Three"));
Console.WriteLine("The SortedList object has a fixed size? = " + list.IsFixedSize);
Console.WriteLine("Does the SortedList object contains key 10? = " + list.ContainsKey("10"));
}
}
출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Key and Value of SortedList.... 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 Is the SortedList having the value? True The SortedList object has a fixed size? = False Does the SortedList object contains key 10? = False
출력 결과에서 볼 수 있듯이, 값 "Three"는 목록에 존재하므로 ContainsValue()가 True를 반환했습니다. 반면 키 "10"은 등록되어 있지 않으므로 ContainsKey()는 False를 반환했으며, 동적으로 크기가 조절되는 일반 SortedList이므로 IsFixedSize 역시 False입니다.
예제 2: 열거자(Enumerator)로 순회하며 키 검색하기
이번에는 IDictionaryEnumerator를 사용해 SortedList 전체를 순회하고, 존재하는 키와 존재하지 않는 키를 각각 확인해 보겠습니다.
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
sortedList.Add("F", "6");
sortedList.Add("G", "7");
sortedList.Add("H", "8");
sortedList.Add("I", "9");
sortedList.Add("J", "10");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count);
Console.WriteLine("\nEnumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Does the SortedList object contains key M? = " + sortedList.ContainsKey("M"));
Console.WriteLine("Does the SortedList object contains key H? = " + sortedList.ContainsKey("H"));
}
}
출력 결과
실행 결과는 다음과 같습니다.
SortedList elements... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 Count of SortedList key-value pairs = 10 Enumerator to iterate through the SortedList... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 Does the SortedList object contains key M? = False Does the SortedList object contains key H? = True
정리
SortedList는 내부적으로 키를 기준으로 항상 정렬 상태를 유지하므로, 출력 시에도 A부터 J까지 알파벳 순서대로 나타납니다. 키 검색에는 ContainsKey(), 값 검색에는 ContainsValue()를 사용하며, 두 메서드 모두 불리언(Boolean) 값을 반환하므로 조건문과 함께 활용하기 매우 편리합니다. 참고로 ContainsKey()는 Contains() 메서드와 동일하게 동작합니다.