Java. util. bitset (a method for storing massive data)

Source: Internet
Author: User
Tags bitset

Java. util. bitset can be stored by bit.
One byte occupies 8 bits in the computer. Data in Java is stored at least by byte,
For example, an int occupies 4 bytes.
If you encounter a large amount of data, it will inevitably require a large storage space and memory.
Algorithms can be used to reduce the storage space and memory occupied by data.
Java. util. bitset provides such an algorithm.
For example, there are a bunch of numbers that need to be stored, source = [3, 5, 6, 9]
Int requires 4*4 bytes.
Java. util. bitset can save true/false.
If Java. util. bitset is used, it will be much less. The principle is:
1. First find the maximum value in the Data maxvalue = 9
2. Declare a bitset BS. Its size is maxvalue + 1 = 10.
3. Traverse data source. Set BS [source [I] to true.
The last value is:
(0 is false; 1 is true)
BS [,]
3, 5, 6, 9

The Int type requires a number of 32 characters in 4 bytes. Currently, only one digit is used!
Proportion 32: 1
This saves a lot of space.

 

 

Test example

package com;import java.util.BitSet;public class MainTestThree {/** * @param args */public static void main(String[] args) {BitSet bm=new BitSet();System.out.println(bm.isEmpty()+"--"+bm.size());bm.set(0);System.out.println(bm.isEmpty()+"--"+bm.size());bm.set(1);System.out.println(bm.isEmpty()+"--"+bm.size());System.out.println(bm.get(65));System.out.println(bm.isEmpty()+"--"+bm.size());bm.set(65);System.out.println(bm.isEmpty()+"--"+bm.size());}}

Output:
True -- 64
False -- 64
False -- 64
False
False -- 64
False -- 128
 
It indicates that the default constructor declares a 64-bit bitset with a value of false.
If the number of bits you want to use exceeds the default size, it will apply for 64 bits instead of an error.

package com;import java.util.BitSet;public class MianTestFour {/** * @param args */public static void main(String[] args) {BitSet bm1=new BitSet(7);System.out.println(bm1.isEmpty()+"--"+bm1.size());BitSet bm2=new BitSet(63);System.out.println(bm2.isEmpty()+"--"+bm2.size());BitSet bm3=new BitSet(65);System.out.println(bm3.isEmpty()+"--"+bm3.size());BitSet bm4=new BitSet(111);System.out.println(bm4.isEmpty()+"--"+bm4.size());}}

 

Output:
True -- 64
True -- 64
True -- 128
True -- 128

It means that all the bits you applied for are multiples of 64. That is to say, if you apply for a bit no more than 64, the value is calculated as 64.
If the number is 2, it is 128.

 

Package com; import Java. util. bitset; public class maintestfive {/*** @ Param ARGs */public static void main (string [] ARGs) {int [] Shu =, 25, 31, 28, 37}; bitset bm1 = new bitset (maintestfive. getmaxvalue (SHU); system. out. println ("bm1.size () --" + bm1.size (); maintestfive. putvalueintobitset (Shu, bm1); printbitset (bm1);} // All values are false at the beginning. You can skip this because falsepublic static void initbitset (bitset BS) {fo R (INT I = 0; I <BS. size (); I ++) {BS. set (I, false) ;}}// print public static void printbitset (bitset BS) {stringbuffer Buf = new stringbuffer (); Buf. append ("[\ n"); For (INT I = 0; I <BS. size (); I ++) {if (I <BS. size ()-1) {Buf. append (maintestfive. getbitto10 (BS. get (I) + ",");} else {Buf. append (maintestfive. getbitto10 (BS. get (I);} If (I + 1) % 8 = 0 & I! = 0) {Buf. append ("\ n") ;}} Buf. append ("]"); system. out. println (BUF. tostring ();} // find the maximum value of the data set public static int getmaxvalue (INT [] zu) {int temp = 0; temp = zu [0]; for (INT I = 0; I <zu. length; I ++) {If (temp <zu [I]) {temp = zu [I] ;}} system. out. println ("maxvalue:" + temp); Return temp;} // put the public static void putvalue1_bitset (INT [] Shu, bitset BS) {for (INT I = 0; I <Shu. length; I ++) {BS. set (Shu [I], true) ;}// true, false to 1, 0 to look good public static string getbitto10 (Boolean flag) {string a = ""; if (flag = true) {return "1" ;}else {return "0 ";}}}

 

Output:
Maxvalue: 42
Bm1.size () -- 64
[
0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0,
,
0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
]

This completes the storage value and value.
Note that it filters repeated numbers. That is to say, if a number appears more than twice, it is recorded as 1.
The information about the number of occurrences is lost.

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.