前言

Bloom过滤器,通过将字符串映射为信息指纹从而节省了空间。Bloom过滤器的原理为,将一个字符串通过一定算法映射为八个Hash值,将八个Hash值对应位置的Bitset位进行填充。在进行校验的时候,通过同样的算法计算八个Hash值,八个Hash值全部存在才可以认定为该字符串在集合中存在。 四个Hash值的布隆过滤器:

2018-01-07-20-52-37.png 通过这样的一个算法,可以无需将字符串的MD5值存储在内存中,只需要定义一定长度的Bitset即可,从而大大节约了空间。

应用场景

Bloom过滤器可以广泛应用于判断集合中是否存在某个元素的大量数据场景,比如黑名单、爬虫访问记录。

实现

关于布隆过滤器的实现,代码如下:

import java.util.BitSet;

/*

简单的Bloom过滤器实现

*/

public class BloomDemo {

private static final int SIZE = 1<<24;

BitSet bitSet=new BitSet(SIZE);

Hash[] hashs=new Hash[8];

private static final int seeds[]=new int[]{3,5,7,9,11,13,17,19};

public static void main(String[] args) {

String email="zhenlingcn@126.com";

BloomDemo bloomDemo=new BloomDemo();

System.out.println(email+"是否在列表中: "+bloomDemo.contains(email));

bloomDemo.add(email);

System.out.println(email+"是否在列表中: "+bloomDemo.contains(email));

email="zhenlingcn@163.com";

System.out.println(email+"是否在列表中: "+bloomDemo.contains(email));

}

public BloomDemo(){

for (int i = 0; i < seeds.length; i++) {

hashs[i]=new Hash(seeds[i]);

}

}

public void add(String string){

for(Hash hash:hashs){

bitSet.set(hash.getHash(string),true);

}

}

public boolean contains(String string){

boolean have=true;

for(Hash hash:hashs){

have&=bitSet.get(hash.getHash(string));

}

return have;

}

class Hash{

private int seed = 0;

public Hash(int seed){

this.seed=seed;

}

public int getHash(String string){

int val=0;

int len=string.length();

for (int i = 0; i < len; i++) {

val=val*seed+string.charAt(i);

}

return val&(SIZE-1);

}

}

}

上面的Hash算法采用了最简单的基于seed和ascii码的Hash算法。八个种子均采用质数,减少HASH碰撞的概率。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐