CopyOnWriteArraySet equals() method in Java

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:

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [32, 67, 98, 100]
Both are Equal

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


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

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.