當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ConcurrentSkipListSet equals()用法及代碼示例


java.util.concurrent.ConcurrentSkipListSet的equals()方法是Java中的一個內置函數,該函數將指定對象與此設置的對象進行相等性比較。如果指定對象也是集合,則返回True。如果滿足以下所有條件,則這兩個集合將相等。

  • 兩組尺寸相同。
  • 指定集中的每個成員都包含在此集中。

此定義確保equals方法可在set接口的不同實現中正常工作。

用法:


ConcurrentSkipListSet.equals(Object o)

參數:該函數返回單個參數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<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-



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 ConcurrentSkipListSet equals() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。