import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
public class GuavaBloomFilter {
public static void main(String[] args){int total =1000000;// default false positive ratefpp0.03// fpp:There will always be a false positive rate in a Bloom filter
// Because hash collisions are impossible to avoid 100%.
// Bloom filter calls this misjudgment rate false positive probability,abbreviated as fpp
BloomFilter<CharSequence> bf = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), total);// Initialize the total bar data into the filter
for (int i =0; i < total; i++){
bf.put(""+ i);}// Determine whether the value exists in the filter
intcount=0;
for (int i =0; i < total +10000; i++){
if (bf.mightContain(""+ i)){count++;}}
System.out.println("Matched quantity "+count);// Specified misjudgment rate:1/10,000 to improve matching accuracy
BloomFilter<CharSequence> bfWithFpp = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), total,0.0001);
for (int i =0; i < total; i++){
bfWithFpp.put(""+ i);}int countFpp =0;
for (int i =0; i < total +10000; i++){
if (bfWithFpp.mightContain(""+ i)){
countFpp++;}}//The smaller the value of the false positive rate fpp
// the higher the matching accuracy.
// When the value of the false positive rate fpp is reduced
// the storage space required is also larger
// Therefore,in actual use,// a trade-off needs to be made between the false positive rate and the storage space.
System.out.println("The specified false positive rate has matched the number "+ countFpp);//(1000001-1000000)/(1000000+10000)*100 ≈ 0.0001}}