Home > 类库下载 > java类库 > Usage of BitSet under Java.util package

Usage of BitSet under Java.util package

阿神
Release: 2016-11-07 17:37:48
Original
2230 people have browsed it

BitSet: It is a byte set, which can be used to represent integers and perform operations such as intersection and union of two sets.

We know that the smallest unit of computer storage is bit, and the smallest unit we use in java programs is byte Byte. The conversion relationship between them is 1Byte=8bit. BitSet represents the size of the integer data based on the location of the bits. For example: 16 is in the 16th position, 5 is in the 5th position, so the following BitSet can represent the set {5,16}

Usage of BitSet under Java.util package

By analyzing the source code of BitSet, we know that BitSet provides two constructors, One is BitSet(), without parameters, and the other is BitSet(int nbits); if we use the constructor without parameters to create a BitSet object, an object with a default length of 64 bits will be created, and the data size that this object can represent It is 1~64, but it doesn't matter even if we put data greater than 64, because BitSet is self-increasing, and the maximum value can be Integer.MAX_VALUE=2147483647.

It is not difficult to understand its function once you know the representation method. For example, if the union of the above set and {2,23,48} is found, it becomes a bit-coded OR operation.

00000000 00000000 00000000 00000000 00000000 00000000 10000000 00100000 Set {5,16}

00000000 00000000 100000 00 00000000 00000000 01000000 00000000 00000010 The result of the OR operation of the set {2,23,48}

is

00000000 00000000 10000000 00000000 00000 000 01000000 10000000 00100010, so the result is {2,5,16,23,48}

Other larger numbers are nothing more than longer bit codes.

Note:

If you create a BitSet set with a maximum number of digits, BitSet bitSet = new BitSet(Integer.MAX_VALUE);

The following error will be reported

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

at java.util.BitSet.initWords(BitSet.java:144)

at java.util.BitSet.(BitSet.java:139)

at Test.main(Test.java:10)

This shows that the java heap space is insufficient and can be changed as follows:

Usage of BitSet under Java.util package

Other reports of java.lang.OutOfMemoryError: Java heap space errors can also be changed in this way


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template