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

Kiểm tra hai đối tượng SortedSet có bằng nhau trong C#

Giới thiệu

Lớp SortedSet<T> trong namespace System.Collections.Generic đại diện cho một tập hợp các phần tử duy nhất được sắp xếp theo thứ tự. Để kiểm tra hai SortedSet có bằng nhau về nội dung, ta không thể sử dụng phương thức Equals() vì nó so sánh tham chiếu. Thay vào đó, ta nên sử dụng SetEquals() hoặc so sánh từng phần tử.

Ví dụ 1

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet set1 = new SortedSet();
        set1.Add(100);
        set1.Add(200);
        set1.Add(300);
        set1.Add(400);

        Console.WriteLine("Các phần tử trong SortedSet1...");
        foreach (int res in set1)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("SortedSet1 có chứa phần tử 400? = " + set1.Contains(400));

        SortedSet set2 = new SortedSet();
        set2.Add(100);
        set2.Add(200);
        set2.Add(300);
        set2.Add(400);

        Console.WriteLine("Các phần tử trong SortedSet2...");
        foreach (int res in set2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("SortedSet2 có chứa phần tử 500? = " + set2.Contains(500));
        Console.WriteLine("Hai tập hợp có bằng nhau? = " + set1.Equals(set2)); // So sánh tham chiếu
        Console.WriteLine("Hai tập hợp có cùng nội dung? = " + set1.SetEquals(set2)); // So sánh nội dung
    }
}

Kết quả

Các phần tử trong SortedSet1...
100
200
300
400
SortedSet1 có chứa phần tử 400? = True
Các phần tử trong SortedSet2...
100
200
300
400
SortedSet2 có chứa phần tử 500? = False
Hai tập hợp có bằng nhau? = False
Hai tập hợp có cùng nội dung? = True

Ví dụ 2

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet set1 = new SortedSet();
        set1.Add(10);
        set1.Add(15);
        set1.Add(30);
        set1.Add(50);
        set1.Add(75);
        set1.Add(100);
        set1.Add(150);

        Console.WriteLine("Các phần tử trong SortedSet1...");
        foreach (int res in set1)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("SortedSet1 có chứa phần tử 400? = " + set1.Contains(400));

        SortedSet set2 = new SortedSet();
        set2.Add(25);
        set2.Add(50);
        set2.Add(100);
        set2.Add(200);
        set2.Add(400);
        set2.Add(500);

        Console.WriteLine("Các phần tử trong SortedSet2...");
        foreach (int res in set2)
        {
            Console.WriteLine(res);
        }

        // Gán set2 = set1 (cùng tham chiếu)
        set2 = set1;
        Console.WriteLine("Hai tập hợp có bằng nhau (tham chiếu)? = " + set1.Equals(set2));
        Console.WriteLine("Hai tập hợp có cùng nội dung? = " + set1.SetEquals(set2));
    }
}

Kết quả

Các phần tử trong SortedSet1...
10
15
30
50
75
100
150
SortedSet1 có chứa phần tử 400? = False
Các phần tử trong SortedSet2...
25
50
100
200
400
500
Hai tập hợp có bằng nhau (tham chiếu)? = True
Hai tập hợp có cùng nội dung? = True

Lưu ý quan trọng

  • Phương thức Equals() mặc định so sánh tham chiếu (reference equality).
  • Sử dụng SetEquals() để so sánh nội dung của hai tập hợp.
  • SortedSet tự động loại bỏ phần tử trùng lặp và duy trì thứ tự sắp xếp.
  • Để so sánh hai tập hợp bất kỳ (không phải SortedSet), có thể dùng LINQ: set1.SequenceEqual(set2) sau khi sắp xếp.