BitMap 用于查重..只能查数字

本文介绍了一个简单的 Java 实现的 BitMap 类,该类使用字节数组来存储位信息。文章展示了如何添加、检查和移除特定位置的位标记,并提供了一个示例程序验证 BitMap 的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码 复制代码  收藏代码
  1. package ansj.sun.util;   
  2. public class BitMap {   
  3.   
  4.     private static final byte MAX = 127;   
  5.   
  6.     public static void main(String[] args) throws InterruptedException {   
  7.         int m = 1578015112 ;   
  8.            
  9.         BitMap hm = new BitMap() ;   
  10.            
  11.         hm.add(m) ;   
  12.            
  13.         System.out.println(hm.contains(m));   
  14.     }   
  15.   
  16.     public BitMap() {   
  17.         bytes = new byte[12500000];   
  18.     }   
  19.   
  20.     public BitMap(int size) {   
  21.         bytes = new byte[size];   
  22.     }   
  23.   
  24.     private byte[] bytes = null;   
  25.   
  26.     public void add(int i) {   
  27.         int r = i / 8;   
  28.         int c = i % 8;   
  29.         bytes[r] = (byte) (bytes[r] | (1 << c));   
  30.     }   
  31.   
  32.     public boolean contains(int i) {   
  33.         int r = i / 8;   
  34.         int c = i % 8;   
  35.         if (((byte) ((bytes[r] >>> c)) & 1) == 1) {   
  36.             return true;   
  37.         }   
  38.         return false;   
  39.     }   
  40.   
  41.     public void remove(int i) {   
  42.         int r = i / 8;   
  43.         int c = i % 8;   
  44.         bytes[r] = (byte) (bytes[r] & (((1 << (c + 1)) - 1) ^ MAX));   
  45.     }   
  46.   
  47. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值