Java Utility Library

Java BitSet - stream() Method



The java.util.BitSet.stream() method returns a stream of indices for which this BitSet contains a bit in the set state. The indices are returned in order, from lowest to highest. The size of the stream is the number of bits in the set state, equal to the value returned by the cardinality() method.

The bit set must remain constant during the execution of the terminal stream operation. Otherwise, the result of the terminal stream operation is undefined.

Syntax

public IntStream stream()

Parameters

No parameter is required.

Return Value

Returns a stream of integers representing set indices.

Exception

NA

Example:

In the example below, the java.util.BitSet.stream() method returns a stream of indices for which the given BitSet contains a bit in the set state.

import java.util.*;
import java.util.stream.IntStream;

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

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

    //printing the BitSet
    System.out.println("BSet contains: " + BSet);

    //Creating an IntStream 
    IntStream IntStrm = BSet.stream(); 
  
    //Using IntStream
    System.out.println("The IntStream is: " + IntStrm); 
    System.out.println("The size of the IntStream: " + IntStrm.count()); 
  }
}

The output of the above code will be:

BSet contains: {10, 20, 30, 40, 50}
The IntStream is: java.util.stream.IntPipeline$Head@548c4f57
The size of the IntStream: 5

❮ Java.util - BitSet