1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.yzw.arithmetic.first;
import java.util.HashSet; import java.util.Random;
public class BitMap {
private long[] bytes = null;
public BitMap(int max) {
bytes = new long[((max + 64) >> 6)]; }
public void add(int number) { bytes[number >> 6] |= (1L << (number & 63)); }
public void del(int number) { bytes[number >> 6] &= ~(1L << (number & 63)); }
public Boolean countains(int number) { return (bytes[number >> 6] & (1L << (number & 63))) != 0;
}
public static void main(String[] args) { int max = 900000; BitMap bitMap = new BitMap(max); HashSet<Integer> hashSet = new HashSet<>(); for (int i = 0; i < max; i++) { double random = Math.random();
int i1 = new Random().nextInt(max);
if (random < 0.3333) { bitMap.add(i1); hashSet.add(i1); }
if (random > 0.3333 && random < 0.6666) { bitMap.del(i1); hashSet.remove(i1); }
if (random > 0.6666) { if (bitMap.countains(i1) != hashSet.contains(i1)) { System.out.println("出错了"); } }
} hashSet.forEach(v -> { if (!bitMap.countains(v)) System.out.println("出错了"); });
}
}
|