快速使用布隆过滤器
pom.xml
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency>
//布隆过滤器 public static void main(String[] args) { String[] a ={"一","二","三","四","五"}; List<String> strings = Arrays.asList(a); //申明布隆过滤器(使用的过滤器,数据长度,误判率越小越精准) BloomFilter<CharSequence> bf = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8),strings.size(),0.03); // 初始化 stings的数据"添加"到过滤器中,这个添加不上真的把数据添加到过滤器中;需要了解详情看下方链接 for (int i = 0; i < strings.size(); i++) { bf.put(strings.get(i)); } //储存过滤出来的数据 ArrayList<String> strings2 = new ArrayList<>(); //需要被过滤的数据 String[] b = {"一","二","三","四","五","六","七","八","九","十"}; List<String> strings1 = Arrays.asList(b); //遍历输出存在过滤器中的数据 for (int i = 0; i <strings1.size() ; i++) { // 判断值是否存在过滤器中 if (!bf.mightContain(strings1.get(i))) { strings2.add(strings1.get(i)); } } System.out.println(strings2.toString());//输出结果[六, 七, 八, 九, 十],说明一,二,三,四,五通过了过滤器 } }
https://zhuanlan.zhihu.com/p/94433082