Phương thức SortedDictionary<TKey, TValue>.ContainsValue() trong C# được dùng để xác định xem SortedDictionary có chứa phần tử với giá trị (value) được chỉ định hay không.
Cú pháp
public bool ContainsValue(TValue value);
value: Giá trị cần tìm kiếm trong SortedDictionary.
Trả về: true nếu tìm thấy giá trị; false nếu không.
Ví dụ 1: Tìm giá trị "Notebook"
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng = " + sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng (cập nhật) = " + sortedDict.Count);
// Kiểm tra ContainsValue
Console.WriteLine("Có giá trị 'Notebook'? = " + sortedDict.ContainsValue("Notebook"));
sortedDict.Clear();
Console.WriteLine("\nSố lượng sau Clear = " + sortedDict.Count);
}
}
Kết quả
Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Số lượng = 4 Các cặp key-value SAU KHI CẬP NHẬT... 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 Số lượng (cập nhật) = 6 Có giá trị 'Notebook'? = True Số lượng sau Clear = 0
Ví dụ 2: Tìm giá trị không tồn tại ("Eleven")
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
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("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng = " + sortedDict.Count);
sortedDict.Add(7, "Seven");
sortedDict.Add(8, "Eight");
Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng (cập nhật) = " + sortedDict.Count);
Console.WriteLine("Có giá trị 'Eleven'? = " + sortedDict.ContainsValue("Eleven"));
sortedDict.Clear();
Console.WriteLine("\nSố lượng sau Clear = " + sortedDict.Count);
}
}
Kết quả
Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Số lượng = 6 Các cặp key-value SAU KHI CẬP NHẬT... 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 Số lượng (cập nhật) = 8 Có giá trị 'Eleven'? = False Số lượng sau Clear = 0
ContainsValue vs ContainsKey
| Phương thức | Tìm kiếm | Phức độ |
|---|---|---|
ContainsKey(key) |
Theo khóa | O(log n) - cây nhị phân |
ContainsValue(value) |
Theo giá trị | O(n) - duyệt tuyến tính |
Lưu ý
ContainsValuechậm hơnContainsKeyvì phải duyệt toàn bộ (O(n)).- Sử dụng
EqualityComparer<TValue>.Defaultđể so sánh giá trị. - Cho phép
valuelà null (reference type). - Nếu cần tìm kiếm giá trị thường xuyên, cân nhắc duy trì
Dictionary<TValue, TKey>ngược (reverse dictionary) hoặc dùngLookup.