ConcurrentSkipListSet equals() method in Java
Last Updated :
17 Sep, 2018
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.
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetEqualsExample1 {
public static void main(String[] args)
{
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
set.add( 10 );
set.add( 35 );
set.add( 20 );
set.add( 25 );
NavigableSet<Integer> des_set = set.descendingSet();
if (set.equals(des_set))
System.out.println( "Both the sets are equal" );
else
System.out.println( "Both the sets are not equal" );
System.out.println( "Contents of the set: " + 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
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetEqualsExample2 {
public static void main(String[] args)
{
ConcurrentSkipListSet<Integer>
set1 = new ConcurrentSkipListSet<Integer>();
ConcurrentSkipListSet<Integer>
set2 = new ConcurrentSkipListSet<Integer>();
set1.add( 10 );
set1.add( 35 );
set1.add( 20 );
set1.add( 25 );
set2.add( 35 );
set2.add( 20 );
set2.add( 25 );
if (set1.equals(set2))
System.out.println( "Both the sets are equal" );
else
System.out.println( "Both the sets are not equal" );
System.out.println( "Contents of the first set: " +
set1);
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-
Share your thoughts in the comments
Please Login to comment...