BitSet isEmpty() method in Java

BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.

Prerequisite : Java BitSet | Set 1

This method is used to check if there are any bits that are set to true, in the specified BitSet. This method returns true if this BitSet contains no bits that are set to true.


public boolean isEmpty()

Return Value: This method returns true if this BitSet contains no bits that are set to true. Otherwise it returns false.

Below program illustrate the isEmpty() method BitSet:





// Java program illustrating Bitset
// isEmpty() function.
import java.util.*;
public class GFG {
    public static void main(String[] args)
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet();
        BitSet bs3 = new BitSet();
        /* set is BitSet class method
        expalined in next articles */
        // assign values to bs2
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
        System.out.println("bs3 : " + bs3);
        // Performing isEmpty on bitsets
        System.out.println("Is bs1 empty : "
                           + bs1.isEmpty());
        System.out.println("Is bs2 empty : "
                           + bs2.isEmpty());
        System.out.println("Is bs3 empty : "
                           + bs3.isEmpty());



bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}
bs3 : {}
Is bs1 empty : false
Is bs2 empty : false
Is bs3 empty : true

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.