Introduction to bitset
Class implements a bitvector that increases on demand. Each component of a bit set has a Boolean value. Use a non-negative integer to index bitset bits. You can test, set, or clear each indexed bit. You can use one bitset to modify the content of another bitset through logic and, logic or logical XOR or operation.
By default, the initial values of all bits in the set are false.
Each bit set has a current size, that is, the number of digits of the space currently used by the bit set. Note that this size is related to the implementation of the bit set, so it may be changed as it is implemented. The length of a bit set is related to the logical length of a bit set and is defined regardless of the implementation.
Unless otherwise specified, passing the null parameter to any method in the bitset will cause nullpointerexception.
Without external synchronization, it is unsafe to operate a bitset by multiple threads.
Basic Principles
The value 1 indicates whether a data item has appeared. If the value 0 is not, the value 1 indicates that a data item has appeared. When used, it can be expressed based on whether a value is 0 or not.
A 1 GB space with 8*1024*1024*1024 = 8.58*10 ^ 9bit, which can represent 8.5 billion different numbers.
Use Cases
Common applications are those that require statistics on massive data volumes, such as log analysis and user count statistics.
For example, if no data exists in 4 billion data records, sort 4 billion different data records.
There are now 10 million random numbers in the range of 1 to 0.1 billion. A new algorithm is required to calculate the number between 1 and 0.1 billion that is not in the random number.
Sample Code
Package util; import Java. util. bitset; public class bitsetdemo {private bitset used = new bitset ();/*** calculate the char contained in a string **/Public void contrainchars (string Str) {for (INT I = 0; I <Str. length (); I ++) used. set (Str. charat (I); // set bit for charstringbuilder sb = new stringbuilder (); sb. append ("["); int size = used. size (); system. out. println (size); For (INT I = 0; I <size; I ++) {If (used. get (I) {sb. appe Nd (char) I) ;}} sb. append ("]"); system. out. println (sb. tostring ();}/*** returns an infinite number of prime numbers. A natural number greater than 1 is called a prime number (prime number) if the number except 1 and itself cannot be divisible by other natural numbers (except 0) otherwise, it is called the sum */Public void computeprime () {bitset sieve = new bitset (1024); int size = sieve. size (); For (INT I = 2; I <size; I ++) sieve. set (I); int finalbit = (INT) math. SQRT (sieve. size (); For (INT I = 2; I <finalbit; I ++) if (sieve. get (I) for (Int J = 2 * I; j <size; j + = I) sieve. clear (j); int counter = 0; For (INT I = 1; I <size; I ++) {If (sieve. get (I) {s Ystem. out. printf ("% 5d", I); If (++ counter % 15 = 0) system. out. println () ;}} system. out. println ();}/*** simple use example */Public void simpleexample () {string Names [] = {"Java", "Source", "and ", "Support"}; bitset bits = new bitset (); For (INT I = 0, n = names. length; I <n; I ++) {If (Names [I]. length () % 2) = 0) {bits. set (I) ;}} system. out. println (BITs); system. out. println ("Size:" + bits. size (); system. out. P Rintln ("Length:" + bits. Length (); For (INT I = 0, n = names. length; I <n; I ++) {If (! Bits. get (I) {system. out. println (Names [I] + "is odd") ;}} bitset bites = new bitset (); bites. set (0); bites. set (1); bites. set (2); bites. set (3); bites. andnot (BITs); system. out. println (bites);} public static void main (string ARGs []) {bitsetdemo BS = new bitsetdemo (); BS. contrainchars ("How do you do? Hello, "); BS. computeprime (); BS. simpleexample ();}}
Refer:
Http://blog.csdn.net/haojun186/article/details/8482343