Java ConcurrentSkipListSet equals()方法

Java ConcurrentSkipListSet equals()方法

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

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

这个定义保证了equals方法在集合接口的不同实现中正常工作。

语法

ConcurrentSkipListSet.equals(Object o)

参数: 该函数返回一个参数,即要与这个集合进行平等比较的对象。

返回值: 该函数返回布尔值。如果指定的对象与这个集合相等,它就返回真,否则就返回假。

下面的程序说明了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<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        set.add(10);
        set.add(35);
        set.add(20);
        set.add(25);
  
        // Creating a descending set object
        NavigableSet<Integer> 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<Integer>
            set1 = new ConcurrentSkipListSet<Integer>();
  
        ConcurrentSkipListSet<Integer>
            set2 = new ConcurrentSkipListSet<Integer>();
  
        // 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#equals-java.lang.Object-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程