Open In App

ConcurrentSkipListSet equals() method in Java

Last Updated : 17 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The equals() method of java.util.concurrent.ConcurrentSkipListSet is an inbuilt function in Java which compares the specified object with this set for equality. It returns True if the specified object is also a set. The two sets will said to be equal if they satisfies all of the conditions stated below:

  • The two sets have the same size.
  • Every member of the specified set is contained in this set.

This definition ensures that the equals method works properly across different implementations of the set interface.

Syntax:

ConcurrentSkipListSet.equals(Object o)

Parameters: The function returns a single parameter o i.e. the object to be compared for equality with this set

Return Value: The function returns boolean value. It returns true if the specified object is equal to this set, otherwise returns false.

Below programs illustrate the ConcurrentSkipListSet.equals() method:

Program 1: In this example both the set are equal.




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


Output:

Both the sets are equal
Contents of the set: [10, 20, 25, 35]
Contents of the descending set: [35, 25, 20, 10]

Program 2: In this example both the set are not equal




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


Output:

Both the sets are not equal
Contents of the first set: [10, 20, 25, 35]
Contents of the second set: [20, 25, 35]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads