--- old/src/share/classes/java/util/BitSet.java 2013-04-26 15:39:50.040682971 -0700 +++ new/src/share/classes/java/util/BitSet.java 2013-04-26 15:39:49.892682974 -0700 @@ -29,6 +29,8 @@ 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 @@ -1188,4 +1190,43 @@ 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); + } }