boolean
value. The
bits of a BitSet
are indexed by nonnegative integers.
Individual indexed bits can be examined, set, or cleared. One
BitSet
may be used to modify the contents of another
BitSet
through logical AND, logical inclusive OR, and
logical exclusive OR operations.
By default, all bits in the set initially have the value
false
.
Every bit set has a current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set relates to logical length of a bit set and is defined independently of implementation.
Unless otherwise noted, passing a null parameter to any of the
methods in a BitSet
will result in a
NullPointerException
.
A BitSet
is not safe for multithreaded use without
external synchronization.
Modifier and Type | Field and Description |
---|---|
private static final int | |
private static final int | |
private static final int | |
private static final ObjectStreamField[] | |
private static final long | |
private transient boolean | sizeIsSticky
Whether the size of "words" is user-specified. |
private static final long | |
private long[] | words
The internal field corresponding to the serialField "bits". |
private transient int | wordsInUse
The number of words in the logical size of this BitSet. |
Access | Constructor and Description |
---|---|
public | |
public | BitSet(int
the initial size of the bit set nbits)Creates a bit set whose initial size is large enough to explicitly
represent bits with indices in the range |
private |
Modifier and Type | Method and Description |
---|---|
public void | |
public void | |
public int | Returns: the number of bits set totrue in this BitSet Returns the number of bits set to |
private void | |
private static void | |
public void | |
public void | clear(int
index of the first bit to be cleared fromIndex, int index after the last bit to be cleared toIndex)Sets the bits from the specified |
public void | |
public Object | Returns: a clone of this bit setOverrides java. Cloning this |
private void | ensureCapacity(int
the minimum acceptable number of words. wordsRequired)Ensures that the BitSet can hold enough words. |
public boolean | Returns: true if the objects are the same;
false otherwisethe object to compare with obj)Overrides java. Compares this bit set against the specified object. |
private void | expandTo(int
the index to be accommodated. wordIndex)Ensures that the BitSet can accommodate a given wordIndex, temporarily violating the invariants. |
public void | flip(int
the index of the bit to flip bitIndex)Sets the bit at the specified index to the complement of its current value. |
public void | flip(int
index of the first bit to flip fromIndex, int index after the last bit to flip toIndex)Sets each bit from the specified |
public boolean | Returns: the value of the bit with the specified indexthe bit index bitIndex)Returns the value of the bit with the specified index. |
public BitSet | Returns: a newBitSet from a range of this BitSet index of the first bit to include fromIndex, int index after the last bit to include toIndex)Returns a new |
public int | Returns: the hash code value for this bit setOverrides java. Returns the hash code value for this bit set. |
private void | |
public boolean | Returns: boolean indicating whether thisBitSet intersects
the specified BitSet BitSet to intersect withReturns true if the specified |
public boolean | Returns: boolean indicating whether thisBitSet is emptyReturns true if this |
public int | Returns: the logical size of thisBitSet Returns the "logical size" of this |
public int | Returns: the index of the next clear bitthe index to start checking from (inclusive) fromIndex)Returns the index of the first bit that is set to |
public int | Returns: the index of the next set bit, or-1 if there
is no such bitthe index to start checking from (inclusive) fromIndex)Returns the index of the first bit that is set to |
private int | Returns: the index of the next set bit, or-1 if there
is no such bitthe index to start checking from (inclusive) fromIndex, int the last word index to check (inclusive) toWordIndex)Returns the index of the first bit that is set to |
public void | |
public int | Returns: the index of the previous clear bit, or-1 if there
is no such bitthe index to start checking from (inclusive) fromIndex)Returns the index of the nearest bit that is set to |
public int | Returns: the index of the previous set bit, or-1 if there
is no such bitthe index to start checking from (inclusive) fromIndex)Returns the index of the nearest bit that is set to |
private void | readObject(ObjectInputStream s)
Reconstitute the |
private void | |
public void | |
public void | set(int
a bit index bitIndex, boolean a boolean value to set value)Sets the bit at the specified index to the specified value. |
public void | set(int
index of the first bit to be set fromIndex, int index after the last bit to be set toIndex)Sets the bits from the specified |
public void | set(int
index of the first bit to be set fromIndex, int index after the last bit to be set toIndex, boolean value to set the selected bits to value)Sets the bits from the specified |
public int | Returns: the number of bits currently in this bit setReturns the number of bits of space actually in use by this
|
public IntStream | Returns: a stream of integers representing set indicesReturns a stream of indices for which this |
public byte[] | Returns: a byte array containing a little-endian representation of all the bits in this bit setReturns a new byte array containing all the bits in this bit set. |
public long[] | Returns: a long array containing a little-endian representation of all the bits in this bit setReturns a new long array containing all the bits in this bit set. |
public String | Returns: a string representation of this bit setOverrides java. Returns a string representation of this bit set. |
private void | |
public static BitSet | Returns: aBitSet containing all the bits in the long arraya long array containing a little-endian representation
of a sequence of bits to be used as the initial bits of the
new bit set longs)Returns a new bit set containing all the bits in the given long array. |
public static BitSet | Returns: aBitSet containing all the bits in the buffer in the
specified rangea long buffer containing a little-endian representation
of a sequence of bits between its position and limit, to be
used as the initial bits of the new bit set lb)Returns a new bit set containing all the bits in the given long buffer between its position and limit. |
public static BitSet | Returns: aBitSet containing all the bits in the byte arraya byte array containing a little-endian
representation of a sequence of bits to be used as the
initial bits of the new bit set bytes)Returns a new bit set containing all the bits in the given byte array. |
public static BitSet | Returns: aBitSet containing all the bits in the buffer in the
specified rangea byte buffer containing a little-endian representation
of a sequence of bits between its position and limit, to be
used as the initial bits of the new bit set bb)Returns a new bit set containing all the bits in the given byte buffer between its position and limit. |
private static int | |
private void | writeObject(ObjectOutputStream s)
Save the state of the |
public void |
ADDRESS_BITS_PER_WORD | back to summary |
---|---|
private static final int ADDRESS_BITS_PER_WORD |
BIT_INDEX_MASK | back to summary |
---|---|
private static final int BIT_INDEX_MASK |
BITS_PER_WORD | back to summary |
---|---|
private static final int BITS_PER_WORD |
serialPersistentFields | back to summary |
---|---|
private static final ObjectStreamField[] serialPersistentFields
|
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID
|
sizeIsSticky | back to summary |
---|---|
private transient boolean sizeIsSticky Whether the size of "words" is user-specified. If so, we assume the user knows what he's doing and try harder to preserve it. |
WORD_MASK | back to summary |
---|---|
private static final long WORD_MASK |
words | back to summary |
---|---|
private long[] words The internal field corresponding to the serialField "bits". |
wordsInUse | back to summary |
---|---|
private transient int wordsInUse The number of words in the logical size of this BitSet. |
BitSet | back to summary |
---|---|
public BitSet() Creates a new bit set. All bits are initially |
BitSet | back to summary |
---|---|
public BitSet(int nbits) Creates a bit set whose initial size is large enough to explicitly
represent bits with indices in the range
|
BitSet | back to summary |
---|---|
private BitSet(long[] words) Creates a bit set using words as the internal representation. The last word (if there is one) must be non-zero. |
and | back to summary |
---|---|
public void and(BitSet set) Performs a logical AND of this target bit set with the
argument bit set. This bit set is modified so that each bit in it
has the value
|
andNot | back to summary |
---|---|
public void andNot(BitSet set) Clears all of the bits in this
|
cardinality | back to summary |
---|---|
public int cardinality() Returns the number of bits set to
|
checkInvariants | back to summary |
---|---|
private void checkInvariants() Every public method must preserve these invariants. |
checkRange | back to summary |
---|---|
private static void checkRange(int fromIndex, int toIndex) Checks that fromIndex ... toIndex is a valid range of bit indices. |
clear | back to summary |
---|---|
public void clear(int bitIndex) Sets the bit specified by the index to
|
clear | back to summary |
---|---|
public void clear(int fromIndex, int toIndex) Sets the bits from the specified
|
clear | back to summary |
---|---|
public void clear() Sets all of the bits in this BitSet to
|
clone | back to summary |
---|---|
public Object clone() Overrides java. Cloning this |
ensureCapacity | back to summary |
---|---|
private void ensureCapacity(int wordsRequired) Ensures that the BitSet can hold enough words.
|
equals | back to summary |
---|---|
public boolean equals(Object obj) Overrides java. Compares this bit set against the specified object.
The result is ((BitSet)obj).get(k) == this.get(k)must be true. The current sizes of the two bit sets are not compared. |
expandTo | back to summary |
---|---|
private void expandTo(int wordIndex) Ensures that the BitSet can accommodate a given wordIndex, temporarily violating the invariants. The caller must restore the invariants before returning to the user, possibly using recalculateWordsInUse().
|
flip | back to summary |
---|---|
public void flip(int bitIndex) Sets the bit at the specified index to the complement of its current value.
|
flip | back to summary |
---|---|
public void flip(int fromIndex, int toIndex) Sets each bit from the specified
|
get | back to summary |
---|---|
public boolean get(int bitIndex) Returns the value of the bit with the specified index. The value
is
|
get | back to summary |
---|---|
public BitSet get(int fromIndex, int toIndex) Returns a new
|
hashCode | back to summary |
---|---|
public int hashCode() Overrides java. Returns the hash code value for this bit set.
The hash code depends only on which bits are set within this
The hash code is defined to be the result of the following calculation:
Note that the hash code changes if the set of bits is altered.
|
initWords | back to summary |
---|---|
private void initWords(int nbits) |
intersects | back to summary |
---|---|
public boolean intersects(BitSet set) Returns true if the specified
|
isEmpty | back to summary |
---|---|
public boolean isEmpty() Returns true if this
|
length | back to summary |
---|---|
public int length() Returns the "logical size" of this
|
nextClearBit | back to summary |
---|---|
public int nextClearBit(int fromIndex) Returns the index of the first bit that is set to
|
nextSetBit | back to summary |
---|---|
public int nextSetBit(int fromIndex) Returns the index of the first bit that is set to To iterate over the
|
nextSetBit | back to summary |
---|---|
private int nextSetBit(int fromIndex, int toWordIndex) Returns the index of the first bit that is set to
|
or | back to summary |
---|---|
public void or(BitSet set) Performs a logical OR of this bit set with the bit set
argument. This bit set is modified so that a bit in it has the
value
|
previousClearBit | back to summary |
---|---|
public int previousClearBit(int fromIndex) Returns the index of the nearest bit that is set to
|
previousSetBit | back to summary |
---|---|
public int previousSetBit(int fromIndex) Returns the index of the nearest bit that is set to To iterate over the
|
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Reconstitute the
|
recalculateWordsInUse | back to summary |
---|---|
private void recalculateWordsInUse() Sets the field wordsInUse to the logical size in words of the bit set. Warning This method assumes that the number of words actually in use is less than or equal to the current value of wordsInUse! |
set | back to summary |
---|---|
public void set(int bitIndex) Sets the bit at the specified index to
|
set | back to summary |
---|---|
public void set(int bitIndex, boolean value) Sets the bit at the specified index to the specified value.
|
set | back to summary |
---|---|
public void set(int fromIndex, int toIndex) Sets the bits from the specified
|
set | back to summary |
---|---|
public void set(int fromIndex, int toIndex, boolean value) Sets the bits from the specified
|
size | back to summary |
---|---|
public int size() Returns the number of bits of space actually in use by this
|
stream | back to summary |
---|---|
public IntStream stream() Returns a stream of indices for which this The stream binds to this bit set when the terminal stream operation commences (specifically, the spliterator for the stream is late-binding). If the bit set is modified during that operation then the result is undefined.
|
toByteArray | back to summary |
---|---|
public byte[] toByteArray() Returns a new byte array containing all the bits in this bit set. More precisely, if
|
toLongArray | back to summary |
---|---|
public long[] toLongArray() Returns a new long array containing all the bits in this bit set. More precisely, if
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a string representation of this bit set. For every index
for which this Example: BitSet drPepper = new BitSet();Now drPepper.toString() returns "{} ".
drPepper.set(2);Now drPepper.toString() returns "{2} ".
drPepper.set(4); drPepper.set(10);Now drPepper.toString() returns "{2, 4, 10} ".
|
trimToSize | back to summary |
---|---|
private void trimToSize() Attempts to reduce internal storage used for the bits in this bit set.
Calling this method may, but is not required to, affect the value
returned by a subsequent call to the |
valueOf | back to summary |
---|---|
public static BitSet valueOf(long[] longs) Returns a new bit set containing all the bits in the given long array. More precisely,
This method is equivalent to
|
valueOf | back to summary |
---|---|
public static BitSet valueOf(LongBuffer lb) Returns a new bit set containing all the bits in the given long buffer between its position and limit. More precisely,
The long buffer is not modified by this method, and no reference to the buffer is retained by the bit set.
|
valueOf | back to summary |
---|---|
public static BitSet valueOf(byte[] bytes) Returns a new bit set containing all the bits in the given byte array. More precisely,
This method is equivalent to
|
valueOf | back to summary |
---|---|
public static BitSet valueOf(ByteBuffer bb) Returns a new bit set containing all the bits in the given byte buffer between its position and limit. More precisely,
The byte buffer is not modified by this method, and no reference to the buffer is retained by the bit set.
|
wordIndex | back to summary |
---|---|
private static int wordIndex(int bitIndex) Given a bit index, return word index containing it. |
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Save the state of the
|
xor | back to summary |
---|---|
public void xor(BitSet set) Performs a logical XOR of this bit set with the bit set
argument. This bit set is modified so that a bit in it has the
value
|