📌  相关文章
📜  Java中的 ConcurrentSkipListSet equals() 方法

📅  最后修改于: 2022-05-13 01:55:04.282000             🧑  作者: Mango

Java中的 ConcurrentSkipListSet equals() 方法

Java .util.concurrent.ConcurrentSkipListSet 的 equals() 方法是Java中的一个内置函数,它将指定对象与此集合进行比较是否相等。如果指定的对象也是一个集合,则返回 True。如果满足以下所有条件,则称这两个集合相等:

  • 两组大小相同。
  • 指定集合的每个成员都包含在此集合中。

此定义确保 equals 方法在 set 接口的不同实现中正常工作。

句法:

ConcurrentSkipListSet.equals(Object o)

参数:函数返回单个参数即要与此集合比较是否相等的对象

返回值:函数返回布尔值。如果指定的对象等于此集合,则返回 true,否则返回 false。

下面的程序说明了 ConcurrentSkipListSet.equals() 方法:

程序 1:在这个例子中,两个集合是相等的。

// Java Program Demonstrate equals()
// method of ConcurrentSkipListSet
  
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetEqualsExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add(10);
        set.add(35);
        set.add(20);
        set.add(25);
  
        // Creating a descending set object
        NavigableSet des_set = set.descendingSet();
  
        // Checking if the set and des
        if (set.equals(des_set))
            System.out.println("Both the sets are equal");
        else
            System.out.println("Both the sets are not equal");
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        // Printing the elements of the descending set
        System.out.println("Contents of the descending set: " + 
                                                      des_set);
    }
}
输出:
Both the sets are equal
Contents of the set: [10, 20, 25, 35]
Contents of the descending set: [35, 25, 20, 10]

程序 2:在这个例子中,两个集合都不相等

// Java Program Demonstrate equals()
// method of ConcurrentSkipListSet
  
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetEqualsExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set1 = new ConcurrentSkipListSet();
  
        ConcurrentSkipListSet
            set2 = new ConcurrentSkipListSet();
  
        // Adding elements to first set
        set1.add(10);
        set1.add(35);
        set1.add(20);
        set1.add(25);
  
        // Adding elements to second set
        set2.add(35);
        set2.add(20);
        set2.add(25);
  
        // Checking if the set and des
        if (set1.equals(set2))
            System.out.println("Both the sets are equal");
        else
            System.out.println("Both the sets are not equal");
  
        // Printing the elements of the set
        System.out.println("Contents of the first set: " + 
                                                      set1);
  
        // Printing the elements of the descending set
        System.out.println("Contents of the second set: " + 
                                                      set2);
    }
}
输出:
Both the sets are not equal
Contents of the first set: [10, 20, 25, 35]
Contents of the second set: [20, 25, 35]

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#equals-java.lang.Object-