Java Utility Library

Java BitSet - toString() Method



The java.util.BitSet.toString() method returns a string representation of this BitSet. For every index for which this BitSet contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.

Syntax

public String toString()

Parameters

No parameter is required.

Return Value

Returns string representation of this BitSet.

Exception

NA.

Example:

In the example below, the java.util.BitSet.toString() method returns a string representation of 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); 

    //printing BitSet
    System.out.println("BSet contains: " + BSet);
    
    //convert the BitSet into a string
    String MyStr = BSet.toString();

    //print the string
    System.out.println("MyStr contains: " + MyStr);
  }
}

The output of the above code will be:

BSet contains: {10, 20, 30, 40, 50}
MyStr contains: {10, 20, 30, 40, 50}

❮ Java.util - BitSet