Bitmap java实现

该博客介绍了如何使用Java实现Bitmap数据结构,通过Data.java生成随机数据并存储到文件中。Bitmap在内存中占用约5MB,可以用于在10000000条数据中快速查询特定数据,实现常数时间复杂度的操作,达到节省内存的效果。

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

bitmap

实现

Data.java,随机产生数据,注意数据是写到文件里的

import java.io.*;
import java.util.Random;

/**
 * Created by Jason on 2016/4/18.
 */
public class Data {

    public static void generateData(int size) {
        Random rd = new Random();
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("qq.txt"));
            for (int i=0;i<size;i++) {
                bw.write(Math.abs(rd.nextInt(size))+"");
                bw.write("\n");
            }
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Bitmap.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by Jason on 2016/4/18.
 */
public class BitMap {

    private int array[];
    private final int shift = 5;

    public BitMap(int size) {
        array = new int[size/8+1];
    }

    public void insert(int x) {
        int idx = x >> shift;   // x/32
        int value = x & (1<<shift);  // x%32
        array[idx] |= 1<<value;  // set the valueTH location to 1
    }

    public boolean isExist(int x) {
        int idx = x >> shift;   // x/32
        int value = x & (1<<shift);  // x%32
        int flag = (array[idx] & (1<<value));  // get the valueTH location
        return (flag==1)?true:false;
    }
    //read from file and insert element
    public void insertAll() {
        try {
            BufferedReader br = new BufferedReader(new FileReader("qq.txt"));
            String str = null;
            while ((str=br.readLine())!=null) {
                insert(Integer.valueOf(str));
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int size = 10000000;
        Data.generateData(size);
        Runtime rt = Runtime.getRuntime();
        System.out.println("当前 Java 虚拟机中占用内存量:" + (rt.totalMemory() - rt.freeMemory())/1000/1000 + "M");
        BitMap bitMap = new BitMap(size);
        System.out.println("当前 Java 虚拟机中占用内存量:" + (rt.totalMemory() - rt.freeMemory())/1000/1000 + "M");

        bitMap.insertAll();
        System.out.println("data generate done!");
        System.out.println(bitMap.isExist(size));
    }

}

输出

当前 Java 虚拟机中占用内存量:9M
当前 Java 虚拟机中占用内存量:14M
data generate done!
false

占用内存大小: int array[size/8+1], 4B * 10000000/8 = 5M

应用

判断某个数据是否存在一千万条数据中,使得内存尽量小,此时采用bitmap数据结构,常数时间查询,内存占用小。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值