public class BitSet
extends Object implements Cloneable, Serializable
| java.lang.Object | |
| java.util.BitSet | |
该类实现了根据需要增长的位向量。 位集的每个组件都有一个boolean值。 BitSet的位由非负整数索引。 各个索引位可以被检查,设置或清除。 一个BitSet可以用来修改另一个BitSet的内容,通过逻辑AND,逻辑OR或逻辑异或操作。
默认情况下,集合中的所有位最初都具有值 false 。
每个比特集都具有当前大小,该比特集是当前由该比特集使用的空间的比特数。 请注意,大小与位集的实现有关,所以它可能随实现而改变。 位集的长度与位集的逻辑长度有关,并且与实现无关地定义。
除非另有说明,否则将空参数传递给 BitSet任何方法将导致 NullPointerException 。
在没有外部同步的情况下, BitSet对于多线程应用并不安全。
Public constructors |
|
|---|---|
BitSet() 创建一个新的位集。 |
|
BitSet(int nbits) 创建一个位集,其初始大小足以明确表示索引在 |
|
公共方法(Public methods) |
|
|---|---|
void |
and(BitSet set) 执行此目标位的设置与参数位设置的逻辑 与 。 |
void |
andNot(BitSet set) 清除此 |
int |
cardinality() 返回此 |
void |
clear(int fromIndex, int toIndex) 将指定的 |
void |
clear(int bitIndex) 将索引指定的位设置为 |
void |
clear() 将此BitSet中的所有位设置为 |
Object |
clone() 克隆这 |
boolean |
equals(Object obj) 将此对象与指定的对象进行比较。 |
void |
flip(int bitIndex) 将指定索引处的位设置为其当前值的补码。 |
void |
flip(int fromIndex, int toIndex) 将指定的 |
boolean |
get(int bitIndex) 返回具有指定索引的位的值。 |
BitSet |
get(int fromIndex, int toIndex) 返回由 |
int |
hashCode() 返回此位集的哈希码值。 |
boolean |
intersects(BitSet set) 如果指定的 |
boolean |
isEmpty() 如果此 |
int |
length() 返回 |
int |
nextClearBit(int fromIndex) 返回在指定起始索引处或之后出现的第一个设置为 |
int |
nextSetBit(int fromIndex) 返回在指定起始索引处或之后出现的第一个设置为 |
void |
or(BitSet set) 执行该位集与位集参数的逻辑 或 。 |
int |
previousClearBit(int fromIndex) 返回在指定起始索引处或之前出现的最接近位的索引,该位设置为 |
int |
previousSetBit(int fromIndex) 返回在指定的起始索引处或之前出现的设置为 |
void |
set(int fromIndex, int toIndex, boolean value) 将指定的 |
void |
set(int fromIndex, int toIndex) 将指定的 |
void |
set(int bitIndex) 将指定索引处的位设置为 |
void |
set(int bitIndex, boolean value) 将指定索引处的位设置为指定值。 |
int |
size() 返回此 |
IntStream |
stream() 返回这个 |
byte[] |
toByteArray() 返回包含此位集中所有位的新字节数组。 |
long[] |
toLongArray() 返回包含此位集中所有位的新长数组。 |
String |
toString() 返回此位集的字符串表示形式。 |
static BitSet |
valueOf(long[] longs) 返回包含给定长数组中所有位的新位集。 |
static BitSet |
valueOf(byte[] bytes) 返回包含给定字节数组中所有位的新位集。 |
static BitSet |
valueOf(ByteBuffer bb) 返回包含给定字节缓冲区中位置和极限之间所有位的新位集。 |
static BitSet |
valueOf(LongBuffer lb) 返回一个新的位集,其中包含位置和极限之间给定长缓冲区中的所有位。 |
void |
xor(BitSet set) 使用位设置参数执行该位设置的逻辑 异或 。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
BitSet (int nbits)
创建一个位集,其初始大小足以显式表示索引在0到nbits-1范围内的位。 所有位最初都是false 。
| 参数(Parameters) | |
|---|---|
nbits |
int: the initial size of the bit set |
| 抛出异常(Throws) | |
|---|---|
NegativeArraySizeException |
if the specified initial size is negative |
void and (BitSet set)
执行此目标位的设置与参数位设置的逻辑与 。 该位集被修改,使得其中的每个位的值都为true当且仅当它们最初具有值true并且位设置参数中的对应位也具有值true 。
| 参数(Parameters) | |
|---|---|
set |
BitSet: a bit set |
void andNot (BitSet set)
清除此 BitSet的相应位在指定的 BitSet设置的所有位。
| 参数(Parameters) | |
|---|---|
set |
BitSet: the BitSet with which to mask this BitSet |
int cardinality ()
返回此 BitSet设置为 true的 BitSet 。
| 返回(Returns) | |
|---|---|
int |
the number of bits set to true in this BitSet |
void clear (int fromIndex,
int toIndex)
将从指定的 fromIndex (含)到指定的 toIndex (独占)的位设置为 false 。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: index of the first bit to be cleared |
toIndex |
int: index after the last bit to be cleared |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex |
void clear (int bitIndex)
将索引指定的位设置为 false 。
| 参数(Parameters) | |
|---|---|
bitIndex |
int: the index of the bit to be cleared |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
Object clone ()
克隆这BitSet产生一个新的BitSet是等于它。 该位集合的克隆是另一个位集合,它与该位集合具有完全相同的位,设置为true 。
| 返回(Returns) | |
|---|---|
Object |
a clone of this bit set |
也可以看看:
boolean equals (Object obj)
将此对象与指定的对象进行比较。 结果是true当且仅当参数不是null并且是一个Bitset对象,该对象具有与此位集合设置为true完全相同的一组位。 也就是说,对于每个非负int索引k ,
((BitSet)obj).get(k) == this.get(k)must be true. The current sizes of the two bit sets are not compared.
| 参数(Parameters) | |
|---|---|
obj |
Object: the object to compare with |
| 返回(Returns) | |
|---|---|
boolean |
true if the objects are the same; false otherwise |
也可以看看:
void flip (int bitIndex)
将指定索引处的位设置为其当前值的补码。
| 参数(Parameters) | |
|---|---|
bitIndex |
int: the index of the bit to flip |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
void flip (int fromIndex,
int toIndex)
将指定的每一位 fromIndex (含)到指定 toIndex (独家)为其当前值的补码。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: index of the first bit to flip |
toIndex |
int: index after the last bit to flip |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex |
boolean get (int bitIndex)
返回具有指定索引的位的值。 该值是true如果与索引的比特bitIndex在该当前设置BitSet ; 否则,结果是false 。
| 参数(Parameters) | |
|---|---|
bitIndex |
int: the bit index |
| 返回(Returns) | |
|---|---|
boolean |
the value of the bit with the specified index |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
BitSet get (int fromIndex, int toIndex)
返回由 BitSet从 fromIndex (含)至 toIndex ( fromIndex )的位组成的新的 BitSet 。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: index of the first bit to include |
toIndex |
int: index after the last bit to include |
| 返回(Returns) | |
|---|---|
BitSet |
a new BitSet from a range of this BitSet |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex |
int hashCode ()
返回此位集的哈希码值。 散列码仅取决于此BitSet中设置的位。
散列码被定义为以下计算的结果:
public int hashCode() {
long h = 1234;
long[] words = toLongArray();
for (int i = words.length; --i >= 0; )
h ^= words[i] * (i + 1);
return (int)((h >> 32) ^ h);
} Note that the hash code changes if the set of bits is altered.
| 返回(Returns) | |
|---|---|
int |
the hash code value for this bit set |
boolean intersects (BitSet set)
如果指定的 BitSet任何位设置为 true ,并且在此 BitSet中也设置为 true ,则返回true。
| 参数(Parameters) | |
|---|---|
set |
BitSet: BitSet to intersect with |
| 返回(Returns) | |
|---|---|
boolean |
boolean indicating whether this BitSet intersects the specified BitSet |
boolean isEmpty ()
如果此 BitSet包含设置为 true位,则返回true。
| 返回(Returns) | |
|---|---|
boolean |
boolean indicating whether this BitSet is empty |
int length ()
返回BitSet的“逻辑大小”: BitSet中最高设置位的BitSet加上1。 如果BitSet包含设定位,则返回零。
| 返回(Returns) | |
|---|---|
int |
the logical size of this BitSet |
int nextClearBit (int fromIndex)
返回在指定起始索引处或之后出现的第一个设置为 false位的索引。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: the index to start checking from (inclusive) |
| 返回(Returns) | |
|---|---|
int |
the index of the next clear bit |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
int nextSetBit (int fromIndex)
返回在指定起始索引处或之后出现的第一位设置为true索引。 如果不存在这样的位,则返回-1 。
遍历 true位在 BitSet ,使用以下循环:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
// operate on index i here
}
| 参数(Parameters) | |
|---|---|
fromIndex |
int: the index to start checking from (inclusive) |
| 返回(Returns) | |
|---|---|
int |
the index of the next set bit, or -1 if there is no such bit |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
void or (BitSet set)
执行该位集与位集参数的逻辑或 。 该位集被修改,使得其中的一个位的值为true当且仅当它已经具有值true或位集合参数中的相应位具有值true 。
| 参数(Parameters) | |
|---|---|
set |
BitSet: a bit set |
int previousClearBit (int fromIndex)
返回在指定起始索引处或之前出现的最接近位的索引,该位设置为false 。 如果不存在这样的位,或者如果给出-1作为起始索引,则返回-1 。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: the index to start checking from (inclusive) |
| 返回(Returns) | |
|---|---|
int |
the index of the previous clear bit, or -1 if there is no such bit |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is less than -1 |
int previousSetBit (int fromIndex)
返回在指定起始索引处或之前出现的设置为true的最近位的索引。 如果不存在这样的位,或者如果给出-1作为起始索引,则返回-1 。
遍历 true位在 BitSet ,使用以下循环:
for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
// operate on index i here
}
| 参数(Parameters) | |
|---|---|
fromIndex |
int: the index to start checking from (inclusive) |
| 返回(Returns) | |
|---|---|
int |
the index of the previous set bit, or -1 if there is no such bit |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is less than -1 |
void set (int fromIndex,
int toIndex,
boolean value)
将指定的位 fromIndex (含)到指定 toIndex (不包括)到指定的值。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: index of the first bit to be set |
toIndex |
int: index after the last bit to be set |
value |
boolean: value to set the selected bits to |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex |
void set (int fromIndex,
int toIndex)
将指定的位 fromIndex (含)到指定 toIndex (不包括)至 true 。
| 参数(Parameters) | |
|---|---|
fromIndex |
int: index of the first bit to be set |
toIndex |
int: index after the last bit to be set |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex |
void set (int bitIndex)
将指定索引处的位设置为 true 。
| 参数(Parameters) | |
|---|---|
bitIndex |
int: a bit index |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
void set (int bitIndex,
boolean value)
将指定索引处的位设置为指定值。
| 参数(Parameters) | |
|---|---|
bitIndex |
int: a bit index |
value |
boolean: a boolean value to set |
| 抛出异常(Throws) | |
|---|---|
IndexOutOfBoundsException |
if the specified index is negative |
int size ()
返回此BitSet用于表示位值的实际使用的空间位数。 该集合中的最大元素是大小 - 第一个元素。
| 返回(Returns) | |
|---|---|
int |
the number of bits currently in this bit set |
IntStream stream ()
返回这个BitSet在设置状态中包含一个位的索引流。 指数从最低到最高依次返回。 流的大小是设置状态中的位数,等于cardinality()方法返回的值。
在执行终端流操作期间,该位设置必须保持不变。 否则,终端流操作的结果是未定义的。
| 返回(Returns) | |
|---|---|
IntStream |
a stream of integers representing set indices |
byte[] toByteArray ()
返回包含此位集中所有位的新字节数组。
更确切地说,如果
byte[] bytes = s.toByteArray();
然后bytes.length == (s.length()+7)/8和
s.get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)
全部为n < 8 * bytes.length 。
| 返回(Returns) | |
|---|---|
byte[] |
a byte array containing a little-endian representation of all the bits in this bit set |
long[] toLongArray ()
返回包含此位集中所有位的新长数组。
更确切地说,如果
long[] longs = s.toLongArray();
然后longs.length == (s.length()+63)/64和
s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)
全部为n < 64 * longs.length 。
| 返回(Returns) | |
|---|---|
long[] |
a long array containing a little-endian representation of all the bits in this bit set |
String toString ()
返回此位集的字符串表示形式。 对于此BitSet在设置状态中包含一位的每个索引,该索引的十进制表示形式都包含在结果中。 这些指数按从低到高的顺序排列,用“,”(逗号和空格)分隔,并用大括号包围,从而形成一组整数的通常数学符号。
例:
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}".
| 返回(Returns) | |
|---|---|
String |
a string representation of this bit set |
BitSet valueOf (long[] longs)
返回包含给定长数组中所有位的新位集。
更确切地说,
BitSet.valueOf(longs).get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)
全部为n < 64 * longs.length 。
该方法相当于 BitSet.valueOf(LongBuffer.wrap(longs)) 。
| 参数(Parameters) | |
|---|---|
longs |
long: a long array containing a little-endian representation of a sequence of bits to be used as the initial bits of the new bit set |
| 返回(Returns) | |
|---|---|
BitSet |
|
BitSet valueOf (byte[] bytes)
返回包含给定字节数组中所有位的新位集。
更确切地说,
BitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)
全部为n < 8 * bytes.length 。
该方法相当于 BitSet.valueOf(ByteBuffer.wrap(bytes)) 。
| 参数(Parameters) | |
|---|---|
bytes |
byte: a byte array containing a little-endian representation of a sequence of bits to be used as the initial bits of the new bit set |
| 返回(Returns) | |
|---|---|
BitSet |
|
BitSet valueOf (ByteBuffer bb)
返回包含给定字节缓冲区中位置和极限之间所有位的新位集。
更确切地说,
BitSet.valueOf(bb).get(n) == ((bb.get(bb.position()+n/8) & (1<<(n%8))) != 0)
全部为n < 8 * bb.remaining() 。
该字节缓冲区不会被该方法修改,并且位集不保留对缓冲区的引用。
| 参数(Parameters) | |
|---|---|
bb |
ByteBuffer: a 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 |
| 返回(Returns) | |
|---|---|
BitSet |
|
BitSet valueOf (LongBuffer lb)
返回一个新的位集,其中包含位置和极限之间给定长缓冲区中的所有位。
更确切地说,
BitSet.valueOf(lb).get(n) == ((lb.get(lb.position()+n/64) & (1L<<(n%64))) != 0)
全部为n < 64 * lb.remaining() 。
长缓冲区不会被此方法修改,并且位集不保留对缓冲区的引用。
| 参数(Parameters) | |
|---|---|
lb |
LongBuffer: a 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 |
| 返回(Returns) | |
|---|---|
BitSet |
|
void xor (BitSet set)
使用位设置参数执行该位设置的逻辑异或 。 该位集被修改,以便当且仅当以下语句之一成立时,其中的某一位具有值true :
true, and the corresponding bit in the argument has the value false. false, and the corresponding bit in the argument has the value true. | 参数(Parameters) | |
|---|---|
set |
BitSet: a bit set |