Saturday, January 31, 2026
HomeLanguagesJavaBitSet equals() Method in Java with Examples

BitSet equals() Method in Java with Examples

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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
123 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS