过滤器BloomFilter"/>
布隆过滤器BloomFilter
应用很多,比如防止缓存击穿,垃圾邮件的检测等等
它的优点是比其他算法查询时间快很多,也有缺点是存在误差。
demo
package com.example.demo.util;import com.googlemon.hash.BloomFilter;
import com.googlemon.hash.Funnels;
import org.apachemons.io.Charsets;public class Test {public static void main(String[] args) {int total=1000000;BloomFilter<String> bf=BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8),total,0.0003);for(int i=0;i<total;i++){bf.put(""+i);}int count=0;for(int i=0;i<total+1000;i++){if(bf.mightContain(""+i)){count++;}}System.out.println(count);}
}
BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8),total,0.0003)
0.0003是值误差率,即总数1000000个,结果会有1010000*(0.0003/100)=3.03
输出结果是1000001
更多推荐
布隆过滤器BloomFilter
发布评论