布隆过滤器使用举例
import com.google.common.collect.Lists; import com.google.common.hash.BloomFilter; import com.google.common.hash.Funnels; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Slf4j public class TestBloomFilter { // 定义布隆过滤器 BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), 100, 0.01); @Test public void test() { // 生成样本 for (int i = 1; i <= 100; i++) { bloomFilter.put("" + i); } // 待测数据 List<String> list = Arrays.asList("90", "100", "101", "200"); // 筛选出样本中已存在的数据 List<String> containList = Lists.newArrayList(); containList = list.stream().filter(x -> bloomFilter.mightContain(x)).collect(Collectors.toList()); // containList:[90, 100] log.info("containList:{}", containList.toString()); } }