BitSet toString() Method in Java with Examples
The java.util.BitSet.toString() is an inbuilt method of BitSet class that is used to get a string representation of the bits of the sets in the form of a set of entries separated by “, “. So basically the toString() method is used to convert all the elements of BitSet into String. In addition to this for every index of the bits in the set state, the decimal representation of that index is also included in the result.
Syntax:
Bit_Set.toString()
Parameter: The method does not take any parameters.
Return value: The method returns the set consisting of the string representation of the elements of the given BitSet.
Below programs illustrate the working of java.util.BitSet.toString() method:
Program 1:
// Java code to illustrate toString() import java.util.*; public class BitSet_Demo { public static void main(String args[]) { // Creating an empty BitSet BitSet init_bitset = new BitSet(); // Use set() method to add elements into the Set init_bitset.set(40); init_bitset.set(25); init_bitset.set(31); init_bitset.set(100); init_bitset.set(53); // Displaying the BitSet System.out.println("BitSet: " + init_bitset); // Displaying the toString() working System.out.println("The String representation: " + init_bitset.toString()); } } |
BitSet: {25, 31, 40, 53, 100}
The String representation: {25, 31, 40, 53, 100}
Program 2:
// Java code to illustrate toString() import java.util.*; public class BitSet_Demo { public static void main(String args[]) { // Creating an empty BitSet BitSet init_bitset = new BitSet(); // Use set() method to add elements into the Set init_bitset.set(10); init_bitset.set(20); init_bitset.set(30); init_bitset.set(40); init_bitset.set(50); // Displaying the BitSet System.out.println("BitSet: " + init_bitset); // Displaying the toString() working System.out.println("The String representation: " + init_bitset.toString()); } } |
BitSet: {10, 20, 30, 40, 50}
The String representation: {10, 20, 30, 40, 50}
Recommended Posts:
- BitSet toLongArray() Method in Java with Examples
- BitSet hashCode Method in Java with Examples
- BitSet length() Method in Java with Examples
- BitSet clone() Method in Java with Examples
- BitSet size() Method in Java with Examples
- BitSet stream() Method in Java with Examples
- BitSet toByteArray() Method in Java with Examples
- BitSet equals() Method in Java with Examples
- Provider toString() method in Java with Examples
- Path toString() method in Java with Examples
- LocalTime toString() method in Java with Examples
- StringBuilder toString() method in Java with Examples
- Date toString() method in Java with Examples
- Optional toString() method in Java with examples
- Java Signature toString() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

