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

Print this page




1212      * <p>The bit set must remain constant during the execution of the
1213      * terminal stream operation.  Otherwise, the result of the terminal
1214      * stream operation is undefined.
1215      *
1216      * @return a stream of integers representing set indices
1217      * @since 1.8
1218      */
1219     public IntStream stream() {
1220         class BitSetIterator implements PrimitiveIterator.OfInt {
1221             int next = nextSetBit(0);
1222 
1223             @Override
1224             public boolean hasNext() {
1225                 return next != -1;
1226             }
1227 
1228             @Override
1229             public int nextInt() {
1230                 if (next != -1) {
1231                     int ret = next;
1232                     next = nextSetBit(next+1);
1233                     return ret;
1234                 } else {
1235                     throw new NoSuchElementException();
1236                 }
1237             }
1238         }
1239 
1240         return StreamSupport.intStream(
1241                 () -> Spliterators.spliterator(
1242                         new BitSetIterator(), cardinality(),
1243                         Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED),
1244                 Spliterator.SIZED | Spliterator.SUBSIZED |
1245                         Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED,
1246                 false);
1247     }
1248 }


1212      * <p>The bit set must remain constant during the execution of the
1213      * terminal stream operation.  Otherwise, the result of the terminal
1214      * stream operation is undefined.
1215      *
1216      * @return a stream of integers representing set indices
1217      * @since 1.8
1218      */
1219     public IntStream stream() {
1220         class BitSetIterator implements PrimitiveIterator.OfInt {
1221             int next = nextSetBit(0);
1222 
1223             @Override
1224             public boolean hasNext() {
1225                 return next != -1;
1226             }
1227 
1228             @Override
1229             public int nextInt() {
1230                 if (next != -1) {
1231                     int ret = next;
1232                     next = (next == Integer.MAX_VALUE) ? -1 : nextSetBit(next+1);
1233                     return ret;
1234                 } else {
1235                     throw new NoSuchElementException();
1236                 }
1237             }
1238         }
1239 
1240         return StreamSupport.intStream(
1241                 () -> Spliterators.spliterator(
1242                         new BitSetIterator(), cardinality(),
1243                         Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED),
1244                 Spliterator.SIZED | Spliterator.SUBSIZED |
1245                         Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED,
1246                 false);
1247     }
1248 }