src/share/classes/java/util/BitSet.java

Print this page
rev 7009 : 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
Contributed-by: akhil.arora@oracle.com, brian.goetz@oracle.com

*** 27,36 **** --- 27,38 ---- import java.io.*; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.LongBuffer; + import java.util.stream.IntStream; + import java.util.stream.StreamSupport; /** * This class implements a vector of bits that grows as needed. Each * component of the bit set has a {@code boolean} value. The * bits of a {@code BitSet} are indexed by nonnegative integers.
*** 1186,1191 **** --- 1188,1232 ---- } b.append('}'); return b.toString(); } + + /** + * Returns a stream of indices for which this {@code 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 {@link #cardinality()} method. + + * @return a stream of integers representing set indices + * @since 1.8 + */ + public IntStream stream() { + class BitSetIterator implements PrimitiveIterator.OfInt { + int next = nextSetBit(0); + + @Override + public boolean hasNext() { + return next != -1; + } + + @Override + public int nextInt() { + if (next != -1) { + int ret = next; + next = nextSetBit(next+1); + return ret; + } else { + throw new NoSuchElementException(); + } + } + } + + return StreamSupport.intStream( + () -> Spliterators.spliterator( + new BitSetIterator(), cardinality(), + Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED), + Spliterator.SIZED | Spliterator.SUBSIZED | + Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED); + } }