当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。