Talking about Java BitSet usage scenarios and code examples

1、 What is BitSet?

Note: the following contents are from JDK API:

The BitSet class implements an on-demand bit vector. Each component of bit set has a Boolean value. Index the bits of BitSet with non negative integers. Each indexed bit can be tested, set, or cleared. Through logical and, logical or and logical XOR operations, you can use one BitSet to modify the contents of another BitSet.

By default, the initial value of all bits in set is false.

Each bit set has a current size, that is, the number of bits of space currently used by the bit set. Note that this size is related to the implementation of bit set, so it may change depending on the implementation. The length of bit set is related to the logical length of bit set and is defined independent of implementation.

A BitSet class creates a special type of array to hold bit values. The array size in BitSet will increase as needed. This is similar to vector of bits.

This is a traditional class, but it has been completely redesigned in Java 2.

BitSet defines two construction methods.

The first construction method creates a default object:

BitSet()

The second method allows the user to specify the initial size. All bits are initialized to 0.

BitSet(intsize)

2、 Implementation principle of Java BitSet

In Java, the implementation of BitSet is located in Java Util package:

It can be seen that the underlying implementation of BitSet uses long array as the internal storage structure, so the size of BitSet is an integer multiple of the size of long type (64 bits).

It has two constructors:

1. Bitset(): create a new bit set. The default size is 64 bits.

2. BitSet (int nbits): create a bit set whose initial size is sufficient to explicitly represent bits with an index range of 0 to nbits-1.

Note:

1. If the initialization size of BitSet is specified, it will be adjusted to an integer multiple greater than or equal to 64 of this number. For example, for 64 bits, the BitSet size is 1 long, while for 65 bits, the BitSet size is 2 long, that is, 128 bits. Such a provision is mainly for memory alignment, while avoiding the need to deal with special situations and simplify the program.

2: size method of BitSet: returns the number of bits of space actually used when this BitSet represents a bit value. The value is an integer multiple of 64

Length method: returns the "logical size" of this BitSet: the index of the highest setting bit in the BitSet plus 1

3、 Usage scenario

A common application scenario is to perform some statistical work on massive data, such as log analysis, user count statistics, etc.

Before the internship interview in Ali, I was asked a question: there are 10 million random numbers, and the range of random numbers is between 100 million and 100 million. Now it is required to write an algorithm to find the number between 100 million and 100 million that is not in the random number?

Code examples are as follows:

summary

The above is all about the usage scenarios and code examples of Java BitSet in this article. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>