Class BloomFilter<T>

java.lang.Object
cloud.opencode.base.hash.bloom.BloomFilter<T>
Type Parameters:
T - element type | 元素类型
All Implemented Interfaces:
Predicate<T>

public final class BloomFilter<T> extends Object implements Predicate<T>
Bloom filter implementation 布隆过滤器实现

A space-efficient probabilistic data structure for membership testing. Returns no false negatives, but may return false positives.

一种用于成员测试的空间高效概率数据结构。不会返回假阴性,但可能返回假阳性。

Features | 主要功能:

  • Configurable expected insertions and FPP - 可配置的预期插入量和误判率
  • No false negatives guaranteed - 保证无假阴性
  • Serialization support - 序列化支持
  • Merge operation - 合并操作

Usage Examples | 使用示例:

BloomFilter<String> filter = BloomFilter.builder(Funnel.STRING_FUNNEL)
    .expectedInsertions(1_000_000)
    .fpp(0.01)
    .build();

filter.put("item1");
if (filter.mightContain("item1")) {
    // Might be present
}

Security | 安全性:

  • Thread-safe: Optional (via BitArray) - 线程安全: 可选(通过BitArray)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Gets the approximate number of elements inserted 获取大约的已插入元素数量
    long
    Gets the bit array size 获取位数组大小
    static <T> BloomFilterBuilder<T>
    builder(Funnel<? super T> funnel)
    Creates a builder 创建构建器
    double
    Gets the expected false positive probability based on current fill 根据当前填充获取预期的假阳性概率
    static <T> BloomFilter<T>
    fromBytes(byte[] bytes, Funnel<? super T> funnel)
    Deserializes from byte array 从字节数组反序列化
    int
    Gets the number of hash functions 获取哈希函数数量
    Merges another bloom filter into this one 将另一个布隆过滤器合并到此过滤器
    boolean
    mightContain(T element)
    Tests if an element might be in the filter 测试元素是否可能在过滤器中
    boolean
    put(T element)
    Adds an element to the filter 向过滤器添加元素
    int
    putAll(Iterable<? extends T> elements)
    Adds multiple elements to the filter 向过滤器添加多个元素
    boolean
    test(T element)
    Tests if an element might be in the filter 测试元素是否可能在过滤器中
    byte[]
    Serializes to byte array 序列化为字节数组

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Predicate

    and, negate, or
  • Method Details

    • put

      public boolean put(T element)
      Adds an element to the filter 向过滤器添加元素
      Parameters:
      element - the element | 元素
      Returns:
      true if the filter may have been changed | 如果过滤器可能已更改返回true
    • putAll

      public int putAll(Iterable<? extends T> elements)
      Adds multiple elements to the filter 向过滤器添加多个元素
      Parameters:
      elements - the elements | 元素
      Returns:
      number of elements that may have caused changes | 可能导致更改的元素数量
    • test

      public boolean test(T element)
      Tests if an element might be in the filter 测试元素是否可能在过滤器中
      Specified by:
      test in interface Predicate<T>
      Parameters:
      element - the element | 元素
      Returns:
      false means definitely not present, true means possibly present | false表示肯定不存在,true表示可能存在
    • mightContain

      public boolean mightContain(T element)
      Tests if an element might be in the filter 测试元素是否可能在过滤器中
      Parameters:
      element - the element | 元素
      Returns:
      false means definitely not present, true means possibly present | false表示肯定不存在,true表示可能存在
    • expectedFpp

      public double expectedFpp()
      Gets the expected false positive probability based on current fill 根据当前填充获取预期的假阳性概率
      Returns:
      current expected FPP | 当前预期误判率
    • approximateElementCount

      public long approximateElementCount()
      Gets the approximate number of elements inserted 获取大约的已插入元素数量
      Returns:
      approximate element count | 大约元素数量
    • bitSize

      public long bitSize()
      Gets the bit array size 获取位数组大小
      Returns:
      bit size | 位数
    • hashCount

      public int hashCount()
      Gets the number of hash functions 获取哈希函数数量
      Returns:
      hash function count | 哈希函数数量
    • merge

      public BloomFilter<T> merge(BloomFilter<T> other)
      Merges another bloom filter into this one 将另一个布隆过滤器合并到此过滤器
      Parameters:
      other - the other filter | 另一个过滤器
      Returns:
      this filter | 此过滤器
    • toBytes

      public byte[] toBytes()
      Serializes to byte array 序列化为字节数组
      Returns:
      byte array | 字节数组
    • fromBytes

      public static <T> BloomFilter<T> fromBytes(byte[] bytes, Funnel<? super T> funnel)
      Deserializes from byte array 从字节数组反序列化
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      bytes - byte array | 字节数组
      funnel - element funnel | 元素funnel
      Returns:
      bloom filter | 布隆过滤器
      Throws:
      OpenHashException - if bytes are invalid | 如果字节无效则抛出异常
    • builder

      public static <T> BloomFilterBuilder<T> builder(Funnel<? super T> funnel)
      Creates a builder 创建构建器
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      funnel - element funnel | 元素funnel
      Returns:
      builder | 构建器