public class BitSet extends Object implements Serializable, Cloneable
BitSet class implements a
bit array.
Each element is either true or false. A BitSet is created with a given size and grows
automatically if this size is exceeded.| Constructor and Description |
|---|
BitSet()
Creates a new
BitSet with size equal to 64 bits. |
BitSet(int bitCount)
Creates a new
BitSet with size equal to bitCount, rounded up to
a multiple of 64. |
| Modifier and Type | Method and Description |
|---|---|
void |
and(BitSet bs)
Logically ands the bits of this
BitSet with bs. |
void |
andNot(BitSet bs)
Clears all bits in this
BitSet which are also set in bs. |
int |
cardinality()
Returns the number of bits that are
true in this BitSet. |
void |
clear()
Clears all the bits in this
BitSet. |
void |
clear(int index)
Clears the bit at index
index. |
void |
clear(int fromIndex,
int toIndex)
Clears the range of bits
[fromIndex, toIndex). |
Object |
clone()
Creates and returns a copy of this
Object. |
boolean |
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void |
flip(int index)
Flips the bit at index
index. |
void |
flip(int fromIndex,
int toIndex)
Flips the range of bits
[fromIndex, toIndex). |
boolean |
get(int index)
Returns the bit at index
index. |
BitSet |
get(int fromIndex,
int toIndex)
Returns a new
BitSet containing the
range of bits [fromIndex, toIndex), shifted down so that the bit
at fromIndex is at bit 0 in the new BitSet. |
int |
hashCode()
Returns an integer hash code for this object.
|
boolean |
intersects(BitSet bs)
Returns true if
this.and(bs) is non-empty, but may be faster than computing that. |
boolean |
isEmpty()
Returns true if all the bits in this
BitSet are set to false, false otherwise. |
int |
length()
Returns the number of bits up to and including the highest bit set.
|
int |
nextClearBit(int index)
Returns the index of the first bit that is clear on or after
index. |
int |
nextSetBit(int index)
Returns the index of the first bit that is set on or after
index, or -1
if no higher bits are set. |
void |
or(BitSet bs)
Logically ors the bits of this
BitSet with bs. |
int |
previousClearBit(int index)
Returns the index of the first bit that is clear on or before
index, or -1 if
no lower bits are clear or index == -1. |
int |
previousSetBit(int index)
Returns the index of the first bit that is set on or before
index, or -1 if
no lower bits are set or index == -1. |
void |
set(int index)
Sets the bit at index
index to true. |
void |
set(int index,
boolean state)
Sets the bit at index
index to state. |
void |
set(int fromIndex,
int toIndex)
Sets the range of bits
[fromIndex, toIndex). |
void |
set(int fromIndex,
int toIndex,
boolean state)
Sets the range of bits
[fromIndex, toIndex) to state. |
int |
size()
Returns the capacity in bits of the array implementing this
BitSet. |
byte[] |
toByteArray()
Returns a new
byte[] containing a little-endian representation the bits of
this BitSet, suitable for passing to valueOf to reconstruct
this BitSet. |
long[] |
toLongArray()
Returns a new
long[] containing a little-endian representation of the bits of
this BitSet, suitable for passing to valueOf to reconstruct
this BitSet. |
String |
toString()
Returns a string containing a concise, human-readable description of the
receiver: a comma-delimited list of the indexes of all set bits.
|
static BitSet |
valueOf(byte[] bytes)
Equivalent to
BitSet.valueOf(ByteBuffer.wrap(bytes)). |
static BitSet |
valueOf(ByteBuffer byteBuffer)
Returns a
BitSet corresponding to byteBuffer, interpreted as a little-endian
sequence of bits. |
static BitSet |
valueOf(long[] longs)
Equivalent to
BitSet.valueOf(LongBuffer.wrap(longs)), but likely to be faster. |
static BitSet |
valueOf(LongBuffer longBuffer)
Returns a
BitSet corresponding to longBuffer, interpreted as a little-endian
sequence of bits. |
void |
xor(BitSet bs)
Logically xors the bits of this
BitSet with bs. |
public BitSet()
BitSet with size equal to 64 bits.public BitSet(int bitCount)
BitSet with size equal to bitCount, rounded up to
a multiple of 64.NegativeArraySizeException - if bitCount < 0.public Object clone()
ObjectObject. The default
implementation returns a so-called "shallow" copy: It creates a new
instance of the same class and then copies the field values (including
object references) from this instance to the new instance. A "deep" copy,
in contrast, would also recursively clone nested objects. A subclass that
needs to implement this kind of cloning should call super.clone()
to create the new instance and then create deep copies of the nested,
mutable objects.public boolean equals(Object o)
Objecto must represent the same object
as this instance using a class-specific comparison. The general contract
is that this comparison should be reflexive, symmetric, and transitive.
Also, no object reference other than null is equal to null.
The default implementation returns true only if this ==
o. See Writing a correct
equals method
if you intend implementing your own equals method.
The general contract for the equals and Object.hashCode() methods is that if equals returns true for
any two objects, then hashCode() must return the same value for
these objects. This means that subclasses of Object usually
override either both methods or neither of them.
equals in class Objecto - the object to compare this instance with.true if the specified object is equal to this Object; false otherwise.Object.hashCode()public int hashCode()
ObjectObject.equals(java.lang.Object) returns true must return
the same hash code value. This means that subclasses of Object
usually override both methods or neither method.
Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode method
if you intend implementing your own hashCode method.
hashCode in class ObjectObject.equals(java.lang.Object)public boolean get(int index)
index. Indexes greater than the current length return false.IndexOutOfBoundsException - if index < 0.public void set(int index)
index to true.IndexOutOfBoundsException - if index < 0.public void clear(int index)
index.IndexOutOfBoundsException - if index < 0.public void flip(int index)
index.IndexOutOfBoundsException - if index < 0.public BitSet get(int fromIndex, int toIndex)
BitSet containing the
range of bits [fromIndex, toIndex), shifted down so that the bit
at fromIndex is at bit 0 in the new BitSet.IndexOutOfBoundsException - if fromIndex or toIndex is negative, or if
toIndex is smaller than fromIndex.public void set(int index,
boolean state)
index to state.IndexOutOfBoundsException - if index < 0.public void set(int fromIndex,
int toIndex,
boolean state)
[fromIndex, toIndex) to state.IndexOutOfBoundsException - if fromIndex or toIndex is negative, or if
toIndex is smaller than fromIndex.public void clear()
BitSet. This method does not change the capacity.
Use clear if you want to reuse this BitSet with the same capacity, but
create a new BitSet if you're trying to potentially reclaim memory.public void set(int fromIndex,
int toIndex)
[fromIndex, toIndex).IndexOutOfBoundsException - if fromIndex or toIndex is negative, or if
toIndex is smaller than fromIndex.public void clear(int fromIndex,
int toIndex)
[fromIndex, toIndex).IndexOutOfBoundsException - if fromIndex or toIndex is negative, or if
toIndex is smaller than fromIndex.public void flip(int fromIndex,
int toIndex)
[fromIndex, toIndex).IndexOutOfBoundsException - if fromIndex or toIndex is negative, or if
toIndex is smaller than fromIndex.public boolean intersects(BitSet bs)
this.and(bs) is non-empty, but may be faster than computing that.public void and(BitSet bs)
BitSet with bs.public void andNot(BitSet bs)
BitSet which are also set in bs.public void or(BitSet bs)
BitSet with bs.public void xor(BitSet bs)
BitSet with bs.public int size()
BitSet. This is
unrelated to the length of the BitSet, and not generally useful.
Use nextSetBit(int) to iterate, or length() to find the highest set bit.public int length()
size() of the BitSet.public String toString()
"{0,1,8}".public int nextSetBit(int index)
index, or -1
if no higher bits are set.IndexOutOfBoundsException - if index < 0.public int nextClearBit(int index)
index.
Since all bits past the end are implicitly clear, this never returns -1.IndexOutOfBoundsException - if index < 0.public int previousSetBit(int index)
index, or -1 if
no lower bits are set or index == -1.IndexOutOfBoundsException - if index < -1.public int previousClearBit(int index)
index, or -1 if
no lower bits are clear or index == -1.IndexOutOfBoundsException - if index < -1.public boolean isEmpty()
BitSet are set to false, false otherwise.public int cardinality()
true in this BitSet.public static BitSet valueOf(long[] longs)
BitSet.valueOf(LongBuffer.wrap(longs)), but likely to be faster.
This is likely to be the fastest way to create a BitSet because it's closest
to the internal representation.public static BitSet valueOf(LongBuffer longBuffer)
BitSet corresponding to longBuffer, interpreted as a little-endian
sequence of bits. This method does not alter the LongBuffer.public static BitSet valueOf(byte[] bytes)
BitSet.valueOf(ByteBuffer.wrap(bytes)).public static BitSet valueOf(ByteBuffer byteBuffer)
BitSet corresponding to byteBuffer, interpreted as a little-endian
sequence of bits. This method does not alter the ByteBuffer.public long[] toLongArray()
long[] containing a little-endian representation of the bits of
this BitSet, suitable for passing to valueOf to reconstruct
this BitSet.public byte[] toByteArray()
byte[] containing a little-endian representation the bits of
this BitSet, suitable for passing to valueOf to reconstruct
this BitSet.