BitMap介紹

rodertW發表於2019-01-28

問題引出:從億萬級資料中儲存查詢某個資料是否存在?

什麼是Bitmap演算法?百度給了一個簡單易懂的講解:http://baijiahao.baidu.com/s?id=1575038901090600&wfr=spider&for=pc

我們在判斷一個資料是否存在,基本思路是讀出來然後遍歷一遍,判斷Boolean。

 

判斷是否存在


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

public class IsNumberExist {
	private int[] bitmap;
	private int size;
	private int SHIFT = 5;// 2的5次方是32

	public boolean isNumberExist(int number) {
		int bit = number >> SHIFT;
		int index = number & ((1 << SHIFT) - 1);
		return ((1 << index) & bitmap[bit]) != 0;
	}

	public IsNumberExist(int size) {
		this.size = size;
		bitmap = new int[(size >> SHIFT) + 1];
	}

	public void insertDate(int number) {
		int bit = number >> SHIFT;
		int index = number & ((1 << SHIFT) - 1);
		bitmap[bit] = bitmap[bit] | (1 << index);
	}

	public void insertFromTxt(String filename) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String str = null;
			while ((str = br.readLine()) != null) {
				insertDate(Integer.valueOf(str));
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Runtime rt = Runtime.getRuntime();
		System.out.println("當前JVM所佔記憶體:" + (rt.totalMemory() - rt.freeMemory())
				/ 1024 / 1024 + "M");
		IsNumberExist tool = new IsNumberExist(1000000000);
		System.out.println("當前JVM所佔記憶體:" + (rt.totalMemory() - rt.freeMemory())
				/ 1024 / 1024 + "M");
		// Date.makeNumbers(100000000);//生成一億個數到number.txt
		tool.insertFromTxt("numbers.txt");// 使用這個一億個數初始化bitmap的狀態
		System.out.println(tool.isNumberExist(88888888));// 判斷88888888是否在這個檔案中
		System.out.println(tool.isNumberExist(99999999));// 判斷99999999是否在這個檔案中
		System.out.println(tool.isNumberExist(91725151));// 判斷91725151是否在這個檔案中

	}
}

生成資料class


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class Date {
	public static boolean makeNumbers(int size) {
		boolean flag = true;
		Random random = new Random();
		try {
			BufferedWriter bw = new BufferedWriter(
					new FileWriter("numbers.txt"));
			for (int i = 0; i < size; i++) {
				bw.write(String.valueOf(Math.abs(random.nextInt(size))));
				bw.newLine();
			}
			bw.close();
		} catch (IOException e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}

	public static void main(String[] args) {
		System.out.println(makeNumbers(100000000));
	}
}

 

利用位對映原理對大資料排重 --- Java程式碼實現:https://blog.csdn.net/a3192048/article/details/80261699