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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


1169      * drPepper.set(2);</pre>
1170      * Now {@code drPepper.toString()} returns "{@code {2}}".
1171      * <pre>
1172      * drPepper.set(4);
1173      * drPepper.set(10);</pre>
1174      * Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
1175      *
1176      * @return a string representation of this bit set
1177      */
1178     public String toString() {
1179         checkInvariants();
1180 
1181         int numBits = (wordsInUse > 128) ?
1182             cardinality() : wordsInUse * BITS_PER_WORD;
1183         StringBuilder b = new StringBuilder(6*numBits + 2);
1184         b.append('{');
1185 
1186         int i = nextSetBit(0);
1187         if (i != -1) {
1188             b.append(i);
1189             for (i = nextSetBit(i+1); i >= 0; i = nextSetBit(i+1)) {


1190                 int endOfRun = nextClearBit(i);
1191                 do { b.append(", ").append(i); }
1192                 while (++i < endOfRun);
1193             }
1194         }
1195 
1196         b.append('}');
1197         return b.toString();
1198     }
1199 
1200     /**
1201      * Returns a stream of indices for which this {@code BitSet}
1202      * contains a bit in the set state. The indices are returned
1203      * in order, from lowest to highest. The size of the stream
1204      * is the number of bits in the set state, equal to the value
1205      * returned by the {@link #cardinality()} method.
1206      *
1207      * <p>The bit set must remain constant during the execution of the
1208      * terminal stream operation.  Otherwise, the result of the terminal
1209      * stream operation is undefined.
1210      *
1211      * @return a stream of integers representing set indices
1212      * @since 1.8


   1 /*
   2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


1169      * drPepper.set(2);</pre>
1170      * Now {@code drPepper.toString()} returns "{@code {2}}".
1171      * <pre>
1172      * drPepper.set(4);
1173      * drPepper.set(10);</pre>
1174      * Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
1175      *
1176      * @return a string representation of this bit set
1177      */
1178     public String toString() {
1179         checkInvariants();
1180 
1181         int numBits = (wordsInUse > 128) ?
1182             cardinality() : wordsInUse * BITS_PER_WORD;
1183         StringBuilder b = new StringBuilder(6*numBits + 2);
1184         b.append('{');
1185 
1186         int i = nextSetBit(0);
1187         if (i != -1) {
1188             b.append(i);
1189             while (true) {
1190                 if (++i < 0) break;
1191                 if ((i = nextSetBit(i)) < 0) break;
1192                 int endOfRun = nextClearBit(i);
1193                 do { b.append(", ").append(i); }
1194                 while (++i != endOfRun);
1195             }
1196         }
1197 
1198         b.append('}');
1199         return b.toString();
1200     }
1201 
1202     /**
1203      * Returns a stream of indices for which this {@code BitSet}
1204      * contains a bit in the set state. The indices are returned
1205      * in order, from lowest to highest. The size of the stream
1206      * is the number of bits in the set state, equal to the value
1207      * returned by the {@link #cardinality()} method.
1208      *
1209      * <p>The bit set must remain constant during the execution of the
1210      * terminal stream operation.  Otherwise, the result of the terminal
1211      * stream operation is undefined.
1212      *
1213      * @return a stream of integers representing set indices
1214      * @since 1.8