Java Utility Library

Java BitSet - clear() Method



The java.util.BitSet.clear() method is used to set all of the bits in the given BitSet to false.

Syntax

public void clear()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.BitSet.clear() method is used to clear all elements of the BitSet called BSet.

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);

    //printing BitSet
    System.out.println("Before applying clear() method.");
    System.out.println("BSet contains: " + BSet);

    //using clear method to clear content of the BitSet
    BSet.clear();

    //printing BitSet
    System.out.println("\nAfter applying clear() method."); 
    System.out.println("BSet contains: " + BSet);   
  }
}

The output of the above code will be:

Before applying clear() method.
BSet contains: {10, 20, 30, 40, 50}

After applying clear() method.
BSet contains: {}

❮ Java.util - BitSet