What is BitSet.isEmpty() in java?

The BitSet class has a built-in isEmpty() method, which checks whether an instance of the BitSet type contains no bits that are going to be set true.

This built-in BitSet class implements a vector of bits, available at java.util.*.

Syntax


public boolean isEmpty()

Parameters

It does not take any argument value.

Return value

It returns a primitive type boolean value.

  • true: It returns true if the invoking object is empty.
  • false: It returns false if the invoking object is not empty.

Code

As highlighted, we have three BitSet objects i.e., bitset1, bitset2, and bitset3. bitset1 and bitset2 are non-empty. So isEmpty() will return false while bitset3 will return true.

import java.util.BitSet;
import java.util.Random;
class EdPresso {
public static void main( String args[] ) {
// object for Random Class
Random rand = new Random();
// objects of BitSet Class
BitSet bitset1 = new BitSet(10);
BitSet bitset2 = new BitSet(10);
BitSet bitset3 = new BitSet(10);
// set some bits
for(int i = 0; i < 8; i++) {
bitset1.set(rand.nextInt(30));
bitset2.set(rand.nextInt(30));
}
System.out.println("bitset1: ");
System.out.println(bitset1);
System.out.println("\nbitset2: ");
System.out.println(bitset2);
System.out.println("\nbitset3: ");
System.out.println(bitset3);
// isEmpty()
System.out.println("\n isEmpty() is calleed");
System.out.println("\nbitset1: "+ bitset1.isEmpty());
System.out.println("bitset2: "+ bitset2.isEmpty());
System.out.println("bitset3: "+ bitset3.isEmpty());
}
}