Skip to content
Related Articles

Related Articles

BitSet equals() Method in Java with Examples

View Discussion
Improve Article
Save Article
Like Article
  • 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:




// Java code to illustrate equals()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet bit_set1 = new BitSet();
        BitSet bit_set2 = new BitSet();
  
        // Use set() method to add elements into the Set
        bit_set1.set(40);
        bit_set1.set(25);
        bit_set1.set(80);
        bit_set1.set(95);
        bit_set1.set(5);
  
        // Use set() method to add elements into the Set
        bit_set2.set(25);
        bit_set2.set(40);
        bit_set2.set(5);
        bit_set2.set(95);
        bit_set2.set(80);
  
        // Displaying the BitSets
        System.out.println("First BitSet: " + bit_set1);
        System.out.println("Second BitSet: " + bit_set2);
  
        // Checking for equality
        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:




// Java code to illustrate equals()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet bit_set1 = new BitSet();
        BitSet bit_set2 = new BitSet();
  
        // Use set() method to add elements into the Set
        bit_set1.set(40);
        bit_set1.set(25);
        bit_set1.set(80);
        bit_set1.set(95);
        bit_set1.set(5);
  
        // Use set() method to add elements into the Set
        bit_set2.set(10);
        bit_set2.set(20);
        bit_set2.set(30);
        bit_set2.set(40);
        bit_set2.set(50);
  
        // Displaying the BitSets
        System.out.println("First BitSet: " + bit_set1);
        System.out.println("Second BitSet: " + bit_set2);
  
        // Checking for equality
        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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!