BitSet equals() Method in Java with Examples
Last Updated :
27 Dec, 2018
The equals() method of Java BitSet class is used to check for equality between two bitsets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not. The method returns true if the bitsets match else false.
Syntax:
Bit_Set1.equals(Bit_Set2)
Parameters: The method accepts one parameter Bit_Set2 of bitset type and refers to the set whose equality is to be checked with this bit set.
Return Value: The method returns true if the equality holds for both the object set else it returns false.
Below programs illustrate the working of BitSet equals() method in Java.
Program 1:
import java.util.*;
public class BitSet_Demo {
public static void main(String args[])
{
BitSet bit_set1 = new BitSet();
BitSet bit_set2 = new BitSet();
bit_set1.set( 40 );
bit_set1.set( 25 );
bit_set1.set( 80 );
bit_set1.set( 95 );
bit_set1.set( 5 );
bit_set2.set( 25 );
bit_set2.set( 40 );
bit_set2.set( 5 );
bit_set2.set( 95 );
bit_set2.set( 80 );
System.out.println( "First BitSet: " + bit_set1);
System.out.println( "Second BitSet: " + bit_set2);
System.out.println( "Are the sets equal? "
+ bit_set1.equals(bit_set2));
}
}
|
Output:
First BitSet: {5, 25, 40, 80, 95}
Second BitSet: {5, 25, 40, 80, 95}
Are the sets equal? true
Program 2:
import java.util.*;
public class BitSet_Demo {
public static void main(String args[])
{
BitSet bit_set1 = new BitSet();
BitSet bit_set2 = new BitSet();
bit_set1.set( 40 );
bit_set1.set( 25 );
bit_set1.set( 80 );
bit_set1.set( 95 );
bit_set1.set( 5 );
bit_set2.set( 10 );
bit_set2.set( 20 );
bit_set2.set( 30 );
bit_set2.set( 40 );
bit_set2.set( 50 );
System.out.println( "First BitSet: " + bit_set1);
System.out.println( "Second BitSet: " + bit_set2);
System.out.println( "Are the sets equal? "
+ bit_set1.equals(bit_set2));
}
}
|
Output:
First BitSet: {5, 25, 40, 80, 95}
Second BitSet: {10, 20, 30, 40, 50}
Are the sets equal? false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...