Java Utility Library

Java BitSet - size() Method



The java.util.BitSet.size() method returns the number of bits of space actually in use by this BitSet to represent bit values. The maximum element in the set is the size - 1st element.

Syntax

public int size()

Parameters

No parameter is required.

Return Value

Returns the number of bits currently in this BitSet.

Exception

NA

Example:

In the example below, the java.util.BitSet.size() method is used to find out the number of bits currently used in the given BitSet.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a BitSet
    BitSet BSet = new BitSet();

    //populating BitSet
    BSet.set(10);
    BSet.set(20);
    BSet.set(30);
    BSet.set(40);
    BSet.set(50);

    System.out.println("Size of BSet: " + BSet.size());    

    //adding one more element in the BitSet
    BSet.set(100); 

    System.out.println("Now, Size of BSet: " + BSet.size());  
  }
}

The output of the above code will be:

Size of BSet: 64
Now, Size of BSet: 128

❮ Java.util - BitSet