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


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.*;
  29 import java.nio.ByteBuffer;
  30 import java.nio.ByteOrder;
  31 import java.nio.LongBuffer;


  32 
  33 /**
  34  * This class implements a vector of bits that grows as needed. Each
  35  * component of the bit set has a {@code boolean} value. The
  36  * bits of a {@code BitSet} are indexed by nonnegative integers.
  37  * Individual indexed bits can be examined, set, or cleared. One
  38  * {@code BitSet} may be used to modify the contents of another
  39  * {@code BitSet} through logical AND, logical inclusive OR, and
  40  * logical exclusive OR operations.
  41  *
  42  * <p>By default, all bits in the set initially have the value
  43  * {@code false}.
  44  *
  45  * <p>Every bit set has a current size, which is the number of bits
  46  * of space currently in use by the bit set. Note that the size is
  47  * related to the implementation of a bit set, so it may change with
  48  * implementation. The length of a bit set relates to logical length
  49  * of a bit set and is defined independently of implementation.
  50  *
  51  * <p>Unless otherwise noted, passing a null parameter to any of the


1170     public String toString() {
1171         checkInvariants();
1172 
1173         int numBits = (wordsInUse > 128) ?
1174             cardinality() : wordsInUse * BITS_PER_WORD;
1175         StringBuilder b = new StringBuilder(6*numBits + 2);
1176         b.append('{');
1177 
1178         int i = nextSetBit(0);
1179         if (i != -1) {
1180             b.append(i);
1181             for (i = nextSetBit(i+1); i >= 0; i = nextSetBit(i+1)) {
1182                 int endOfRun = nextClearBit(i);
1183                 do { b.append(", ").append(i); }
1184                 while (++i < endOfRun);
1185             }
1186         }
1187 
1188         b.append('}');
1189         return b.toString();







































1190     }
1191 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.*;
  29 import java.nio.ByteBuffer;
  30 import java.nio.ByteOrder;
  31 import java.nio.LongBuffer;
  32 import java.util.stream.IntStream;
  33 import java.util.stream.StreamSupport;
  34 
  35 /**
  36  * This class implements a vector of bits that grows as needed. Each
  37  * component of the bit set has a {@code boolean} value. The
  38  * bits of a {@code BitSet} are indexed by nonnegative integers.
  39  * Individual indexed bits can be examined, set, or cleared. One
  40  * {@code BitSet} may be used to modify the contents of another
  41  * {@code BitSet} through logical AND, logical inclusive OR, and
  42  * logical exclusive OR operations.
  43  *
  44  * <p>By default, all bits in the set initially have the value
  45  * {@code false}.
  46  *
  47  * <p>Every bit set has a current size, which is the number of bits
  48  * of space currently in use by the bit set. Note that the size is
  49  * related to the implementation of a bit set, so it may change with
  50  * implementation. The length of a bit set relates to logical length
  51  * of a bit set and is defined independently of implementation.
  52  *
  53  * <p>Unless otherwise noted, passing a null parameter to any of the


1172     public String toString() {
1173         checkInvariants();
1174 
1175         int numBits = (wordsInUse > 128) ?
1176             cardinality() : wordsInUse * BITS_PER_WORD;
1177         StringBuilder b = new StringBuilder(6*numBits + 2);
1178         b.append('{');
1179 
1180         int i = nextSetBit(0);
1181         if (i != -1) {
1182             b.append(i);
1183             for (i = nextSetBit(i+1); i >= 0; i = nextSetBit(i+1)) {
1184                 int endOfRun = nextClearBit(i);
1185                 do { b.append(", ").append(i); }
1186                 while (++i < endOfRun);
1187             }
1188         }
1189 
1190         b.append('}');
1191         return b.toString();
1192     }
1193 
1194     /**
1195      * Returns a stream of indices for which this {@code BitSet}
1196      * contains a bit in the set state. The indices are returned
1197      * in order, from lowest to highest. The size of the stream
1198      * is the number of bits in the set state, equal to the value
1199      * returned by the {@link #cardinality()} method.
1200 
1201      * @return a stream of integers representing set indices
1202      * @since 1.8
1203      */
1204     public IntStream stream() {
1205         class BitSetIterator implements PrimitiveIterator.OfInt {
1206             int next = nextSetBit(0);
1207 
1208             @Override
1209             public boolean hasNext() {
1210                 return next != -1;
1211             }
1212 
1213             @Override
1214             public int nextInt() {
1215                 if (next != -1) {
1216                     int ret = next;
1217                     next = nextSetBit(next+1);
1218                     return ret;
1219                 } else {
1220                     throw new NoSuchElementException();
1221                 }
1222             }
1223         }
1224 
1225         return StreamSupport.intStream(
1226                 () -> Spliterators.spliterator(
1227                         new BitSetIterator(), cardinality(),
1228                         Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED),
1229                 Spliterator.SIZED | Spliterator.SUBSIZED |
1230                         Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED);
1231     }
1232 }