Java 布隆算法结构

Java 布隆算法结构

public class BloomFilterDemo {

    private Listseeds = Arrays.asList(10,20,30,40,50,60,70,80);

    private ListsimpleHashes =new ArrayList<>();

    private  static final int MAXIMUM_CAPACITY =1 <<30;

    private BitSetbitSet =new BitSet(MAXIMUM_CAPACITY);//32亿大的布隆结构

    public static void main(String[] args) {

            String s ="www.baidus";

               BloomFilterDemo bd =new BloomFilterDemo();

    //bd.addValue(s);

    System.out.println(bd.getValue(s));

    }

public BloomFilterDemo() {

    seeds.stream().forEach(seed ->{

            simpleHashes.add(new SimpleHash(MAXIMUM_CAPACITY,seed));

    });

}

boolean isTrue = Boolean.TRUE;

/**

* 获取布隆值是否存在

* @param value

* @return

*/

public boolean getValue(Object value) {

		simpleHashes.stream().forEach(sh ->{

		isTrue =isTrue &&bitSet.get(sh.key(value));//是true为true 遇到false 全是false

    });

return isTrue;

}

/**

* 放入过滤值到布隆结构中

* @param value

*/

public  void  addValue(Object value) {

	simpleHashes.stream().forEach(simpleHash -> {

	System.out.println(simpleHash.key(value));

	bitSet.set(simpleHash.key(value),true);

	});

}

/**

* 8个Hash因子解决来减少Hash碰撞

*/

public static class SimpleHash {

private  int capacity;

private  int seed;

public SimpleHash(int capacity,int seed) {

this.capacity = capacity;

this.seed = seed;

}

final int hash(Object key) {

int h;

return (key ==null) ?0 : (h = key.hashCode()) ^ (h >>>seed);//HashMap原理 解决hash碰撞 不懂			可以去理解HashMap 源码

    }

 final  int key(Object key) {

        return hash(key) & (capacity-1);

        }

    }

}
上一篇:tf.random.shuffle()函数


下一篇:2021牛客暑期多校训练营1 H题