转载自: https://blog.csdn.net/heshiyuan1406146854/article/details/122687988
/** * byte数组实现的bitmap * * @author SXD * @date 2024/11/21 */ public class MyBitmap { private byte[] buckets; public MyBitmap(int maxNum) { this.init(maxNum); } private void init(int maxNum) { int maxBucketIndex = this.getBucketIndex(maxNum); this.buckets = new byte[maxBucketIndex + 1]; } public void add(int num) { int bucketIndex = this.getBucketIndex(num); int bitIndex = this.getBucketInnerBitIndex(num); this.buckets[bucketIndex] |= 1 << bitIndex; } public boolean contains(int num) { int bucketIndex = this.getBucketIndex(num); int bitIndex = this.getBucketInnerBitIndex(num); return (this.buckets[bucketIndex] & 1 << bitIndex) != 0; } private int getBucketIndex(int num) { return num >> 3; } private int getBucketInnerBitIndex(int num) { return num & 0x07; } }
测试:
MyBitmap bitmap = new MyBitmap(1000); List<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); ids.add(3); ids.add(15); ids.add(23); ids.add(24); ids.add(25); ids.add(30); ids.add(49); ids.add(50); for (Integer id : ids) { bitmap.add(id); for (int i = 1; i <= 50; i++) { if (bitmap.contains(i)) { System.out.println(i + "存在"); } else { System.out.println(i + "不存在"); } } }
标签:java,int,ids,BitMap,add,num,bitmap,byte,位图 From: https://www.cnblogs.com/sxdcgaq8080/p/18561345