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


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


CopyOnWriteArraySet的equals(Object O)方法將指定的對象與此設置的對象進行比較,以確定是否相等。如果指定對象與此對象相同,或者它也是一個Set,並且迭代器在指定集合上返回的元素與迭代器在該集合上返回的元素相同,則返回true。更正式地講,如果兩個迭代器返回相同數量的元素,並且對於指定集合上迭代器返回的每個元素e1,迭代器在該集合上返回的元素e2都將被視為返回相同元素,使得( e1 == null?e2 == null:e1.equals(e2))。

用法:

public boolean equals(Object O)

參數:該函數接受強製性參數Object O,該參數是與CopyOnWriteArraySet進行比較的對象。


返回值:如果比較的對象等於此Set,則該函數返回true。

示例1:

// Java Program to illustrate the 
// equals() method in Java 
  
import java.util.concurrent.CopyOnWriteArraySet; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of CopyOnWriteArraySet 
        CopyOnWriteArraySet<Integer> ArrSet1 
            = new CopyOnWriteArraySet<Integer>(); 
  
        // Add elements 
        ArrSet1.add(32); 
        ArrSet1.add(67); 
        ArrSet1.add(98); 
        ArrSet1.add(100); 
  
        // print CopyOnWriteArraySet 
        System.out.println("CopyOnWriteArraySet 1: "
                           + ArrSet1); 
  
        // create object of CopyOnWriteArraySet 
        CopyOnWriteArraySet<Integer> ArrSet2 
            = new CopyOnWriteArraySet<Integer>(); 
  
        // Add elements 
        ArrSet2.add(32); 
        ArrSet2.add(67); 
        ArrSet2.add(98); 
        ArrSet2.add(100); 
  
        // print CopyOnWriteArraySet 
        System.out.println("CopyOnWriteArraySet 2: "
                           + ArrSet2); 
  
        // compares both using equal() function 
        if (ArrSet1.equals(ArrSet2)) 
            System.out.println("Both are Equal"); 
        else
            System.out.println("Both are not Equal"); 
    } 
}
輸出:
CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [32, 67, 98, 100]
Both are Equal

示例2:

// Java Program to illustrate the 
// equals() method in Java 
  
import java.util.concurrent.CopyOnWriteArraySet; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of CopyOnWriteArraySet 
        CopyOnWriteArraySet<Integer> ArrSet1 
            = new CopyOnWriteArraySet<Integer>(); 
  
        // Add elements 
        ArrSet1.add(32); 
        ArrSet1.add(67); 
        ArrSet1.add(98); 
        ArrSet1.add(100); 
  
        // print CopyOnWriteArraySet 
        System.out.println("CopyOnWriteArraySet 1: "
                           + ArrSet1); 
  
        // create object of CopyOnWriteArraySet 
        CopyOnWriteArraySet<Integer> ArrSet2 
            = new CopyOnWriteArraySet<Integer>(); 
  
        // Add elements 
        ArrSet2.add(3); 
        ArrSet2.add(6); 
        ArrSet2.add(9); 
        ArrSet2.add(10); 
  
        // print CopyOnWriteArraySet 
        System.out.println("CopyOnWriteArraySet 2: "
                           + ArrSet2); 
  
        // compares both using equal() function 
        if (ArrSet1.equals(ArrSet2)) 
            System.out.println("Both are Equal"); 
        else
            System.out.println("Both are not Equal"); 
    } 
}
輸出:
CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [3, 6, 9, 10]
Both are not Equal

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#equals-java.lang.Object-



相關用法


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