Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

CopyOnWriteArraySet equals() method in Java

  • Last Updated : 25 Nov, 2020

The equals(Object O) method of CopyOnWriteArraySet compares the specified object with this set for equality. Returns true if the specified object is the same object as this object, or if it is also a Set and the elements returned by an iterator over the specified set are the same as the elements returned by an iterator over this set. More formally, the two iterators are considered to return the same elements if they return the same number of elements and for every element e1 returned by the iterator over the specified set, there is an element e2 returned by the iterator over this set such that (e1==null ? e2==null : e1.equals(e2)).

Syntax: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public boolean equals(Object O)

Parameters: This function accepts a mandatory parameter Object O which is the Object with which the CopyOnWriteArraySet is to be compared.

Return Value: The function returns true if the compared object is equal to this Set.



Program 1: 

Java




// Java Program to illustrate the
// equals() method in CopyOnWriteArraySet
 
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");
    }
}
Output: 
CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [32, 67, 98, 100]
Both are Equal

 

Program 2: 

Java




// Java Program to illustrate the
// equals() method in CopyOnWriteArraySet
 
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");
    }
}
Output: 
CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [3, 6, 9, 10]
Both are not Equal

 

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!