自己手寫一個BloomFilter

1.什麼是BloomFilter  布隆過濾器

     布隆過濾器用於判斷一個元素是否在一個集合中,它有一定的誤判率,不存在的元素,一定不存在。存在的不一定真的存在,
它使用的是數組,它的空間效率是一般算法的1/8左右

2.BloomFilter 的核心思想是什麼?
     布隆過濾器的核心思想:
     add 操作: 計算k個hash函數的值,把對應的結果映射到位數組上,將相應的位數組上的值值爲1
     contain 操作: 計算k個hash函數的值,判斷k個所有的值是否都爲1,如果都爲1,返回true,如果有一個不等於0 則返回false

自己的實現

/**
 * @ClassName: MyBloomFilter
 * @Description: 布隆過濾器用於判斷一個元素是否在一個集合中,它有一定的誤判率,4
 * 不存在的元素,一定不存在。存在的不一定真的存在,
 * 它使用的是數組,它的空間效率是一般算法的1/8左右
 *  布隆過濾器的核心思想:
 *  add 操作: 計算k個hash函數的值,把對應的結果映射到位數組上,將相應的位數組上的值值爲1
 *  contain 操作: 計算k個hash函數的值,判斷k個所有的值是否都爲1,如果都爲1,返回true,如果有一個不等於0 則返回false
 *  參考:
 *  1.https://www.bilibili.com/video/BV1n5411s7Wb?from=search&seid=7498069184359593981
 *  2.https://www.cnblogs.com/xiaobaituyun/p/11011393.html
 * @Author: fuGuoWen
 * @Date: 2020/5/30 22:36
 * @Version: v1.0 文件初始創建
 */
public class MyBloomFilter {
    private byte[] data;
    private int[] slots;

    /** 初始化容器的大小 */
    public MyBloomFilter(int num) {
        this.data = new byte[num*2];
        slots=new int[3];
    }

    /**
     * @Description: 把對應的元素添加待BloomFilter 
     *
     * @Date: 2020/5/30 22:50
     * @Author: fuguowen
     * @Return 
     * @Throws 
     */
    public void add(Integer key){
        /** 計算hash1的函數取模以後的桶爲 */
        int location1 = Math.abs(hash1(key) % data.length);
        /** 計算hash2的函數取模以後的桶爲 */
        int location2 = Math.abs(hash2(key) % data.length);
        /** 計算hash3的函數取模以後的桶爲 */
        int location3 = Math.abs(hash3(key) % data.length);
        /** 把對應的hash函數的桶位置爲1 */
        data[location1]=1;
        data[location2]=1;
        data[location3]=1;

    }
    /**
     * @Description: 判斷一個元素是否在布隆過濾器中
     *
     * @Date: 2020/5/30 22:28
     * @Author: fuGuoWen
     * @Return java.lang.Boolean  true:在布隆過濾器中  false: 不在布隆過濾器中 
     * @Throws
     */
    public Boolean contain(Integer key){
        int location1 = Math.abs(hash1(key) % data.length);
        int location2 = Math.abs(hash2(key) % data.length);
        int location3 = Math.abs(hash3(key) % data.length);
        /** 只要有一個元素部位1,計算結果爲0,返回false,否則 計算結果爲1,返回true */
        return data[location1]*data[location2]*data[location3]==1;
    }
    /**
     * @Description: hash1 計算key的hash 值
     *
     * @Date: 2020/5/30 22:49
     * @Author: fuGuoWen
     * @Return 
     * @Throws 
     */
    public int hash1(Integer key){
        return key.hashCode();
    }

    /**
     * @Description: hash2 計算key的hash 值

     * @Date: 2020/5/30 22:49
     * @Author: fuGuoWen
     * @Return
     * @Throws
     */
    public int hash2(Integer key){
        return key.hashCode()^(key.hashCode() >>>3);
    }

    /**
     * @Description: hash3 計算key的hash 值
     *
     * @Date: 2020/5/30 22:49
     * @Author: fuGuoWen
     * @Return
     * @Throws
     */
    public int hash3(Integer key){
        return key.hashCode()^(key.hashCode() >>>16);
    }

}

參考:
1.https://www.bilibili.com/video/BV1n5411s7Wb?from=search&seid=7498069184359593981
2.https://www.cnblogs.com/xiaobaituyun/p/11011393.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章