Java代码实现com.google.guava guava23.0
import com.google.common.hash.BloomFilter; import com.google.common.hash.Funnels; import java.nio.charset.Charset; public class Demo01 { public static void main(String[] args) { //参数1:字符编码集, 参数2:加入的key的数量, 参数3: 预期的误判率 BloomFilterbloomFilter = BloomFilter.create(Funnels.integerFunnel(), 10000000, 0.01); //添加数据 bloomFilter.put("张三".hashCode()); bloomFilter.put("李四".hashCode()); //获取存储的数据的条数 long l = bloomFilter.approximateElementCount(); System.out.println("数据的长度"+l); //判断是否存在 System.out.println(bloomFilter.mightContain("张三".hashCode()));//true System.out.println(bloomFilter.mightContain("王五".hashCode()));//false //参数1:字符编码集, 参数2:加入的key的数量, 参数3: 预期的误判率 BloomFilter bloomFilter1 = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 1000000, 0.01); //添加数据 bloomFilter1.put("张三"); bloomFilter1.put("李四"); //判断是否存在 System.out.println(bloomFilter1.mightContain("张三"));//true System.out.println(bloomFilter1.mightContain("王五"));//false } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)