Class BloomFilter<T>
java.lang.Object
cloud.opencode.base.hash.bloom.BloomFilter<T>
- Type Parameters:
T- element type | 元素类型
- All Implemented Interfaces:
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 TypeMethodDescriptionlongGets the approximate number of elements inserted 获取大约的已插入元素数量longbitSize()Gets the bit array size 获取位数组大小static <T> BloomFilterBuilder<T> Creates a builder 创建构建器doubleGets the expected false positive probability based on current fill 根据当前填充获取预期的假阳性概率static <T> BloomFilter<T> Deserializes from byte array 从字节数组反序列化intGets the number of hash functions 获取哈希函数数量merge(BloomFilter<T> other) Merges another bloom filter into this one 将另一个布隆过滤器合并到此过滤器booleanmightContain(T element) Tests if an element might be in the filter 测试元素是否可能在过滤器中booleanAdds an element to the filter 向过滤器添加元素intAdds multiple elements to the filter 向过滤器添加多个元素booleanTests if an element might be in the filter 测试元素是否可能在过滤器中byte[]toBytes()Serializes to byte array 序列化为字节数组
-
Method Details
-
put
Adds an element to the filter 向过滤器添加元素- Parameters:
element- the element | 元素- Returns:
- true if the filter may have been changed | 如果过滤器可能已更改返回true
-
putAll
-
test
-
mightContain
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
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
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
Creates a builder 创建构建器- Type Parameters:
T- element type | 元素类型- Parameters:
funnel- element funnel | 元素funnel- Returns:
- builder | 构建器
-