BitSet stream() Method in Java with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The stream() method of Java BitSet class is used to return a stream of indices for every bit contained in the BitSet. The indices are returned in increasing order. The size of the stream is the number of bits in the set state of the BitSet, which is equal to the value returned by the cardinality() method. Syntax: public IntStream stream() Parameters: The method does not take any parameters. Return Value: The method returns a stream of indices for every bit contained in the BitSet. Below programs illustrate the working of java.util.BitSet.stream() method: Program 1: Java // Java code to illustrate stream() import java.util.*; import java.util.stream.IntStream; 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); // Creating an IntStream IntStream indice_Stream = init_bitset.stream(); // Displaying the working System.out.println("The BitSet: " + init_bitset); System.out.println("The stream of indices: " + indice_Stream); System.out.println("The size of the stream: " + indice_Stream.count()); } } Output: BitSet: {10, 20, 30, 40, 50} The BitSet: {10, 20, 30, 40, 50} The stream of indices: java.util.stream.IntPipeline$Head@4eec7777 The size of the stream: 5 Program 2: Java // Java code to illustrate stream() import java.util.*; import java.util.stream.IntStream; 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(121); init_bitset.set(400); init_bitset.set(2); init_bitset.set(15); init_bitset.set(106); init_bitset.set(55); // Displaying the BitSet System.out.println("BitSet: " + init_bitset); // Creating an IntStream IntStream indice_Stream = init_bitset.stream(); // Displaying the working System.out.println("The BitSet: " + init_bitset); System.out.println("The stream of indices: " + indice_Stream); System.out.println("The size of the stream: " + indice_Stream.count()); } } Output: BitSet: {2, 15, 25, 31, 40, 55, 100, 106, 121, 400} The BitSet: {2, 15, 25, 31, 40, 55, 100, 106, 121, 400} The stream of indices: java.util.stream.IntPipeline$Head@4eec7777 The size of the stream: 10 Comment More infoAdvertise with us Next Article Stream skip() method in Java with examples C chinmoy lenka Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-BitSet +1 More Practice Tags : JavaMisc Similar Reads Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Stream dropWhile() method in Java with examples The dropWhile(java.util.function.Predicate) method returns two different types of stream depending upon whether the stream is ordered or not. If the stream is ordered then a stream of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate i 3 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Stream builder() in Java with Examples Stream builder() returns a builder for a Stream. Syntax : static <T> Stream.Builder<T> builder() where, T is the type of elements. Return Value : A stream builder. Example 1 : Java // Java code for Stream builder() import java.util.*; import java.util.stream.Stream; class GFG { // Driver 2 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read Like