Package java.util
Class BitSet
java.lang.Object
java.util.BitSet
- All Implemented Interfaces:
Serializable,Cloneable
public class BitSet extends Object implements Serializable, Cloneable
The
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.- See Also:
- Serialized Form
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description voidand(BitSet bs)Logically ands the bits of thisBitSetwithbs.voidandNot(BitSet bs)Clears all bits in thisBitSetwhich are also set inbs.intcardinality()Returns the number of bits that aretruein thisBitSet.voidclear()Clears all the bits in thisBitSet.voidclear(int index)Clears the bit at indexindex.voidclear(int fromIndex, int toIndex)Clears the range of bits[fromIndex, toIndex).Objectclone()Creates and returns a copy of thisObject.booleanequals(Object o)Compares this instance with the specified object and indicates if they are equal.voidflip(int index)Flips the bit at indexindex.voidflip(int fromIndex, int toIndex)Flips the range of bits[fromIndex, toIndex).booleanget(int index)Returns the bit at indexindex.BitSetget(int fromIndex, int toIndex)Returns a newBitSetcontaining the range of bits[fromIndex, toIndex), shifted down so that the bit atfromIndexis at bit 0 in the newBitSet.inthashCode()Returns an integer hash code for this object.booleanintersects(BitSet bs)Returns true ifthis.and(bs)is non-empty, but may be faster than computing that.booleanisEmpty()Returns true if all the bits in thisBitSetare set to false, false otherwise.intlength()Returns the number of bits up to and including the highest bit set.intnextClearBit(int index)Returns the index of the first bit that is clear on or afterindex.intnextSetBit(int index)Returns the index of the first bit that is set on or afterindex, or -1 if no higher bits are set.voidor(BitSet bs)Logically ors the bits of thisBitSetwithbs.intpreviousClearBit(int index)Returns the index of the first bit that is clear on or beforeindex, or -1 if no lower bits are clear orindex == -1.intpreviousSetBit(int index)Returns the index of the first bit that is set on or beforeindex, or -1 if no lower bits are set orindex == -1.voidset(int index)Sets the bit at indexindexto true.voidset(int index, boolean state)Sets the bit at indexindextostate.voidset(int fromIndex, int toIndex)Sets the range of bits[fromIndex, toIndex).voidset(int fromIndex, int toIndex, boolean state)Sets the range of bits[fromIndex, toIndex)tostate.intsize()Returns the capacity in bits of the array implementing thisBitSet.byte[]toByteArray()Returns a newbyte[]containing a little-endian representation the bits of thisBitSet, suitable for passing tovalueOfto reconstruct thisBitSet.long[]toLongArray()Returns a newlong[]containing a little-endian representation of the bits of thisBitSet, suitable for passing tovalueOfto reconstruct thisBitSet.StringtoString()Returns a string containing a concise, human-readable description of the receiver: a comma-delimited list of the indexes of all set bits.static BitSetvalueOf(byte[] bytes)Equivalent toBitSet.valueOf(ByteBuffer.wrap(bytes)).static BitSetvalueOf(long[] longs)Equivalent toBitSet.valueOf(LongBuffer.wrap(longs)), but likely to be faster.static BitSetvalueOf(ByteBuffer byteBuffer)Returns aBitSetcorresponding tobyteBuffer, interpreted as a little-endian sequence of bits.static BitSetvalueOf(LongBuffer longBuffer)Returns aBitSetcorresponding tolongBuffer, interpreted as a little-endian sequence of bits.voidxor(BitSet bs)Logically xors the bits of thisBitSetwithbs.
-
Constructor Details
-
BitSet
public BitSet()Creates a newBitSetwith size equal to 64 bits. -
BitSet
public BitSet(int bitCount)Creates a newBitSetwith size equal tobitCount, rounded up to a multiple of 64.- Throws:
NegativeArraySizeException- ifbitCount < 0.
-
-
Method Details
-
clone
Description copied from class:ObjectCreates and returns a copy of thisObject. 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 callsuper.clone()to create the new instance and then create deep copies of the nested, mutable objects. -
equals
Description copied from class:ObjectCompares this instance with the specified object and indicates if they are equal. In order to be equal,omust 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
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.- Overrides:
equalsin classObject- Parameters:
o- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
get
public boolean get(int index)Returns the bit at indexindex. Indexes greater than the current length return false.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
set
public void set(int index)Sets the bit at indexindexto true.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
clear
public void clear(int index)Clears the bit at indexindex.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
flip
public void flip(int index)Flips the bit at indexindex.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
get
Returns a newBitSetcontaining the range of bits[fromIndex, toIndex), shifted down so that the bit atfromIndexis at bit 0 in the newBitSet.- Throws:
IndexOutOfBoundsException- iffromIndexortoIndexis negative, or iftoIndexis smaller thanfromIndex.
-
set
public void set(int index, boolean state)Sets the bit at indexindextostate.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
set
public void set(int fromIndex, int toIndex, boolean state)Sets the range of bits[fromIndex, toIndex)tostate.- Throws:
IndexOutOfBoundsException- iffromIndexortoIndexis negative, or iftoIndexis smaller thanfromIndex.
-
clear
public void clear()Clears all the bits in thisBitSet. This method does not change the capacity. Useclearif you want to reuse thisBitSetwith the same capacity, but create a newBitSetif you're trying to potentially reclaim memory. -
set
public void set(int fromIndex, int toIndex)Sets the range of bits[fromIndex, toIndex).- Throws:
IndexOutOfBoundsException- iffromIndexortoIndexis negative, or iftoIndexis smaller thanfromIndex.
-
clear
public void clear(int fromIndex, int toIndex)Clears the range of bits[fromIndex, toIndex).- Throws:
IndexOutOfBoundsException- iffromIndexortoIndexis negative, or iftoIndexis smaller thanfromIndex.
-
flip
public void flip(int fromIndex, int toIndex)Flips the range of bits[fromIndex, toIndex).- Throws:
IndexOutOfBoundsException- iffromIndexortoIndexis negative, or iftoIndexis smaller thanfromIndex.
-
intersects
Returns true ifthis.and(bs)is non-empty, but may be faster than computing that. -
and
Logically ands the bits of thisBitSetwithbs. -
andNot
Clears all bits in thisBitSetwhich are also set inbs. -
or
Logically ors the bits of thisBitSetwithbs. -
xor
Logically xors the bits of thisBitSetwithbs. -
size
public int size()Returns the capacity in bits of the array implementing thisBitSet. This is unrelated to the length of theBitSet, and not generally useful. UsenextSetBit(int)to iterate, orlength()to find the highest set bit. -
length
public int length()Returns the number of bits up to and including the highest bit set. This is unrelated to thesize()of theBitSet. -
toString
Returns a string containing a concise, human-readable description of the receiver: a comma-delimited list of the indexes of all set bits. For example:"{0,1,8}". -
nextSetBit
public int nextSetBit(int index)Returns the index of the first bit that is set on or afterindex, or -1 if no higher bits are set.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
nextClearBit
public int nextClearBit(int index)Returns the index of the first bit that is clear on or afterindex. Since all bits past the end are implicitly clear, this never returns -1.- Throws:
IndexOutOfBoundsException- ifindex < 0.
-
previousSetBit
public int previousSetBit(int index)Returns the index of the first bit that is set on or beforeindex, or -1 if no lower bits are set orindex == -1.- Throws:
IndexOutOfBoundsException- ifindex < -1.- Since:
- 1.7
-
previousClearBit
public int previousClearBit(int index)Returns the index of the first bit that is clear on or beforeindex, or -1 if no lower bits are clear orindex == -1.- Throws:
IndexOutOfBoundsException- ifindex < -1.- Since:
- 1.7
-
isEmpty
public boolean isEmpty()Returns true if all the bits in thisBitSetare set to false, false otherwise. -
cardinality
public int cardinality()Returns the number of bits that aretruein thisBitSet. -
valueOf
Equivalent toBitSet.valueOf(LongBuffer.wrap(longs)), but likely to be faster. This is likely to be the fastest way to create aBitSetbecause it's closest to the internal representation.- Since:
- 1.7
-
valueOf
Returns aBitSetcorresponding tolongBuffer, interpreted as a little-endian sequence of bits. This method does not alter theLongBuffer.- Since:
- 1.7
-
valueOf
Equivalent toBitSet.valueOf(ByteBuffer.wrap(bytes)).- Since:
- 1.7
-
valueOf
Returns aBitSetcorresponding tobyteBuffer, interpreted as a little-endian sequence of bits. This method does not alter theByteBuffer.- Since:
- 1.7
-
toLongArray
public long[] toLongArray()Returns a newlong[]containing a little-endian representation of the bits of thisBitSet, suitable for passing tovalueOfto reconstruct thisBitSet.- Since:
- 1.7
-
toByteArray
public byte[] toByteArray()Returns a newbyte[]containing a little-endian representation the bits of thisBitSet, suitable for passing tovalueOfto reconstruct thisBitSet.- Since:
- 1.7
-