Principle Introduction:
The Java platform's BitSet is used to hold a bit sequence, and if you want to store a bit sequence efficiently, you can use a bit set (BitSet). Because bit sets wrap bits in bytes, using a bit set is more efficient and saves more storage space than using a list of Boolean objects.
Bitset is a bitwise operation of the object, the value is only 0 or 1 is false and true, the internal maintenance of a long array, initially only a long, so bitset the smallest size is 64, when with the storage of more and more elements, bitset inside will dynamically expand, one time to expand 64 bits , and the final interior is stored by n a long.
By default, all bits of Bitset are false, which is 0.
In the absence of external synchronization, it is not safe for multiple threads to operate a bitset.
A 1GB space, with 8*1024*1024*1024 = 8.58*10^9bit, that is, 1GB of space can represent more than 8.5 billion numbers.
Application Scenarios:
1. Statistics of a group of large data not appearing in the number;
Map this set of data to Bitset, and then traverse Bitset, which corresponds to a number with a bit of 0 indicating that no data has occurred.
2. Sort the big data;
The data is mapped to Bitset, and the traversal Bitset gets the ordered data.
3. Compress large data in memory and so on.
A GB of memory space can store more than 8.5 billion numbers, can effectively achieve data compression storage, saving memory space overhead.
Why does Bitset use a long array for internal storage?
The JDK chooses a long array as the internal storage structure of the Bitset for performance reasons, because Bitset provides and and or, it requires that all bit bits in the two bitset be done with and or or, and all array elements need to be traversed when implemented. Using long can minimize the number of loops, so Java chooses to use a long array as the internal storage structure of the bitset.
From the storage of data on the stack, there is no difference between the use of long and byte, in addition to the compiler to force the address alignment, the use of a byte up to 7 bytes (forcing the address to the 8 in multiples), and also from the memory reading group elements, there is no difference, Because assembly instructions have MOV instructions for different lengths of data. So the root cause of the JDK choosing to use a long array as the internal storage structure of Bitset is to reduce the number of cycles and improve performance when and and OR.
For example, we do bitset in the and, Or,xor operation, to the entire bitset in the bit to operate, we need to read out all of the bitset word, if it is a long array storage, we can read into 64 bits each time, and the int array is stored, Only 32 bits can be read at a time. In addition, when we look for the next bit of 1 in Bitset, Word first compares with 0, and if Word has a value of 0, it means that Word does not have a 1 bit, which can be ignored, and if it is a long array store, you can skip 64 bit at a time, If an int array is stored, only 32 bits can be skipped at a time.
(Source of this paragraph: http://www.zhihu.com/question/21061816)
BitSet API
BitSet() Creates a new bit set. |
BitSet(int nbits) Creates a bit set whose initial size is sufficient to explicitly represent the index range 0 to nbits-1 the bit. |
void |
and(BitSet set) Performs logic and operation on this target bit set and parameter bit set. |
void |
andNot(BitSet set) Clears BitSet all bits in this, and its corresponding bits are set in the specified BitSet . |
int |
cardinality() Returns the BitSet number of digits in this set to true . |
void |
clear() Set all the bits in this BitSet to false . |
void |
clear(int bitIndex) Sets the bit in the index at the specified position to false . |
void |
clear(int fromIndex, int toIndex) Sets the specified FromIndex(including) to a bit in the specified Toindex(not included) range false . |
Object |
clone() Copy this BitSet and generate a new one that is equal to it BitSet . |
boolean |
equals(Object obj) Compares this object to the specified object. |
void |
flip(int bitIndex) Sets the bit at the specified index to the complement of its current value. |
void |
flip(int fromIndex, int toIndex) Sets the specified FromIndex(inclusive) to the complement of its current value for each bit within the specified Toindex(not included) range. |
boolean |
get(int bitIndex) Returns the bit value at the specified index. |
BitSet |
get(int fromIndex, int toIndex) Returns a new BitSetthat is composed of bits from FromIndex(including) to Toindex(not included) in the range from BitSet . |
int |
hashCode() Returns the hash code value for this bit set. |
boolean |
intersects(BitSet set) If there is BitSet a bit set to in the specified true , and BitSet It is also set to true , the ture is returned. |
boolean |
isEmpty() If this BitSet does not contain any bits set to true , the ture is returned. |
int |
length() Returns the BitSet "logical size" of this: the index of the BitSet highest set bit in the 1 plus. |
int |
nextClearBit(int fromIndex) Returns the index of the first bit that is set to false , which occurs at the specified starting index or after the index. |
int |
nextSetBit(int fromIndex) Returns the index of the first bit that is set to true , which occurs at the specified starting index or after the index. |
void |
or(BitSet set) Performs a logical OR operation on this bit set and bit set parameter. |
void |
set(int bitIndex) Sets the bit at the specified index to true . |
void |
set(int bitIndex, boolean value) Sets the bit at the specified index to the specified value. |
void |
set(int fromIndex, int toIndex) Sets the specified FromIndex(including) to a bit in the specified Toindex(not included) range true . |
void |
set(int fromIndex, int toIndex, boolean value) Sets the specified FromIndex(inclusive) to the specified value in the specified Toindex(not included) range of bits. |
int |
size() Returns the BitSet number of bits of space that are actually used when this represents a bit value. |
String |
toString() Returns the string representation of this bit set. |
void |
xor(BitSet set) Performs a logical XOR operation on this bit set and bit set parameters. |
Java BitSet (bit set)