bloomFilter_demo
参考博客:(14条消息) 布隆(Bloom Filter)过滤器入门_布隆过滤器入门_qq_39093474的博客-CSDN博客
5 分钟搞懂布隆过滤器,亿级数据过滤算法你值得拥有! - 知乎 (zhihu.com)
BloomFilterTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.hmb; import com.google.common.hash.BloomFilter; import com.google.common.hash.Funnels; import java.util.HashSet; import java.util.Set; public class BloomFilterTest { private static Integer EXPECTED_INSERT_NUM = 10000000 ; private static double fpp = 0.01 ; private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), EXPECTED_INSERT_NUM, fpp); private static Set<Integer> set = new HashSet<>(); public static void main(String[] args) { long stime = System.currentTimeMillis(); for ( int i = 0 ; i < EXPECTED_INSERT_NUM; i++) { bloomFilter.put(i); } int count = 0 ; for ( int i = EXPECTED_INSERT_NUM; i < EXPECTED_INSERT_NUM * 2 ; i++) { if (bloomFilter.mightContain(i)) { count++; } } long etime = System.currentTimeMillis(); System.out.println( "incorrect judge num: " + count); System.out.println( "spend time: " + (etime - stime) + " ms" ); stime = System.currentTimeMillis(); for ( int i = 0 ; i < EXPECTED_INSERT_NUM; i++) { set.add(i); } count = 0 ; for ( int i = EXPECTED_INSERT_NUM; i < EXPECTED_INSERT_NUM * 2 ; i++) { if (set.contains(i)) { count++; } } etime = System.currentTimeMillis(); System.out.println( "incorrect judge num: " + count); System.out.println( "spend time: " + (etime - stime) + " ms" ); } } |
笔者拿bloomFilter跟set进行了对比,当EXPECTED_INSERT_NUM<=10000000时,发现都是set更快,当EXPECTED_INSERT_NUM==100000000时,set就会报OOM了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?