Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Kiểm tra SortedList có chứa giá trị cụ thể trong C#

Giới thiệu

Để kiểm tra xem một SortedList có chứa một giá trị (value) cụ thể hay không, sử dụng phương thức ContainsValue(). SortedList lưu trữ cặp key-value, sắp xếp theo key.

Cú pháp

public virtual bool ContainsValue (object value);

Ví dụ 1: Giá trị tồn tại

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 và Value của SortedList....");
        foreach(DictionaryEntry entry in list) {
            Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);
        }

        Console.WriteLine("SortedList có chứa giá trị 'Five'? " + list.ContainsValue("Five"));
    }
}

Đầu ra ví dụ 1

Key và Value của 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
SortedList có chứa giá trị 'Five'? True

Ví dụ 2: Giá trị không tồn tại

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 và Value của SortedList....");
        foreach(DictionaryEntry entry in list) {
            Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);
        }

        Console.WriteLine("SortedList có chứa giá trị 'Tenth'? " + list.ContainsValue("Tenth"));
    }
}

Đầu ra ví dụ 2

Key và Value của 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
SortedList có chứa giá trị 'Tenth'? False

Lưu ý quan trọng

  • ContainsValue() thực hiện tìm kiếm tuyến tính (linear search) O(n) vì SortedList chỉ index theo key.
  • Sử dụng Equals() để so sánh giá trị (value equality).
  • Trả về true nếu tìm thấy ít nhất một key có value khớp (value có thể trùng lặp).
  • Để kiểm tra key, dùng ContainsKey() - O(log n) nhờ binary search trên key.

So sánh ContainsKey vs ContainsValue

Phương thứcĐộ phức tạpTìm kiếm
ContainsKey(key)O(log n)Binary search trên key đã sắp xếp
ContainsValue(value)O(n)Linear search qua tất cả value

SortedList vs SortedDictionary vs Dictionary

Đặc điểmSortedListSortedDictionaryDictionary
Sắp xếpLuôn theo keyLuôn theo keyKhông (chèn thứ tự)
ContainsKeyO(log n)O(log n)O(1)
ContainsValueO(n)O(n)O(n)
MemoryÍt hơn (mảng)Nhiều hơn (tree)Trung bình
Insert/RemoveO(n) (dịch mảng)O(log n)O(1)

Khuyến nghị

  • Dùng SortedList khi dữ liệu ít thay đổi (load once, read many), cần tiết kiệm bộ nhớ.
  • Dùng SortedDictionary khi thường xuyên insert/remove.
  • Dùng Dictionary khi không cần sắp xếp, cần hiệu năng cao nhất.
  • Generic versions: SortedList<TKey, TValue>, SortedDictionary<TKey, TValue>.