Class BloomFilter<E>
- Type Parameters:
E- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>
- Direct Known Subclasses:
CountingBloomFilter
This implementation uses Murmur3.murmur3_x86_32 as hash source is backed by a
BitSet.
Although this class implements Set, not all set operations are actually supported (e.g.
removal or retrieval of elements).
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionBloomFilter(int n, double p, int m, int k, Function<E, byte[]> bytesSupplier) Create a new bloom filter.BloomFilter(int n, double p, int m, int k, Function<E, byte[]> bytesSupplier, BitSet bitSet) Create a new bloom filter.BloomFilter(Integer n, Double p, Function<E, byte[]> bytesSupplier) Create a new bloom filter.Create a new bloom filter.protectedBloomFilter(BloomFilter.Parameters parameters, Function<E, byte[]> bytesSupplier) protectedBloomFilter(BloomFilter.Parameters parameters, Function<E, byte[]> bytesSupplier, BitSet bitSet) -
Method Summary
Modifier and TypeMethodDescriptionbooleanAddselementto the filter.booleanaddAll(Collection<? extends E> c) bitset()Returns theBitSetholding the internal bloom filter state.voidclear()booleanChecks ifois contained in the filter.booleancontainsAll(Collection<?> c) protected booleangetBit(int index) protected int[]Calculateskhashes forelement.booleanisEmpty()iterator()Not supported.intk()Returns the number of hash functions.intm()Returns the number of bits in the filter.voidmerge(BloomFilter<E> other) Merges this filter withother.intn()Returns the number of items in the filter.doublep()Returns the probability of false positives, fraction between 0 and 1.booleanNot supported.booleanremoveAll(Collection<?> c) Not supported.booleanretainAll(Collection<?> c) Not supported.protected booleansetBit(int index) intsize()Not supported.Object[]toArray()Not supported.<T> T[]toArray(T[] a) Not supported.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
equals, hashCode, spliterator
-
Field Details
-
parameters
-
bitSet
-
bytesSupplier
-
-
Constructor Details
-
BloomFilter
protected BloomFilter(BloomFilter.Parameters parameters, Function<E, byte[]> bytesSupplier, BitSet bitSet) -
BloomFilter
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0as it must be derived from the other parameters.Visit Bloom Filter Calculator to get more information about the implications/calculation of these parameters.
If
mis not a multiple of MurmurHash3 x86 32-bit hash, this bloom filter is subject of the "modulo bias" effect.- Parameters:
n- Number of items in the filter. If0, this value is derived fromp,m, andkp- Probability of false positives, fraction between 0 and 1. If0, this value is derived fromn,m, andkm- Number of bits in the filter. If0, this value is derived fromnandpk- Number of hash functions. If0, this value is derived frommandnbytesSupplier- aFunctionthat convert objects of typeEtobyte[]arraysbitSet-BitSetholding this bloom filter's state. Caller must ensure that it has the correct size.- Throws:
IllegalArgumentException- if one parameter is not0orpis not between 0 and 1NullPointerException- ifbytesSupplierisnull
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0as it must be derived from the other parameters.Visit Bloom Filter Calculator to get more information about the implications/calculation of these parameters.
If
mis not a multiple of MurmurHash3 x86 32-bit hash, this bloom filter is subject of the "modulo bias" effect.- Parameters:
n- Number of items in the filter. If0, this value is derived fromp,m, andkp- Probability of false positives, fraction between 0 and 1. If0, this value is derived fromn,m, andkm- Number of bits in the filter. If0, this value is derived fromnandpk- Number of hash functions. If0, this value is derived frommandnbytesSupplier- aFunctionthat convert objects of typeEtobyte[]arrays- Throws:
IllegalArgumentException- if one parameter is not0orpis not between 0 and 1NullPointerException- ifbytesSupplierisnull
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0as it must be derived from the other parameters.Visit Bloom Filter Calculator to get more information about the implications/calculation of these parameters.
- Parameters:
n- Number of items in the filterp- Probability of false positives, fraction between 0 and 1bytesSupplier- aFunctionthat convert objects of typeEtobyte[]arraysbitSet-BitSetholding this bloom filter's state. Caller must ensure that it has the correct size.- Throws:
IllegalArgumentException- ifnorpis0orpis not between 0 and 1NullPointerException- ifbytesSupplierisnull
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0as it must be derived from the other parameters.Visit Bloom Filter Calculator to get more information about the implications/calculation of these parameters.
- Parameters:
n- Number of items in the filterp- Probability of false positives, fraction between 0 and 1bytesSupplier- aFunctionthat convert objects of typeEtobyte[]arrays- Throws:
IllegalArgumentException- ifnorpis0orpis not between 0 and 1NullPointerException- ifbytesSupplierisnull
-
-
Method Details
-
toString
-
size
public int size()Not supported. ThrowsUnsupportedOperationException.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
isEmpty
public boolean isEmpty() -
contains
Checks ifois contained in the filter. False positive messages may occur. -
iterator
Not supported. ThrowsUnsupportedOperationException.- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
toArray
Not supported. ThrowsUnsupportedOperationException.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
toArray
public <T> T[] toArray(T[] a) Not supported. ThrowsUnsupportedOperationException.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
add
Addselementto the filter. -
remove
Not supported. ThrowsUnsupportedOperationException.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>
-
addAll
-
retainAll
Not supported. ThrowsUnsupportedOperationException.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
removeAll
Not supported. ThrowsUnsupportedOperationException.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Returns:
- throws
UnsupportedOperationException - Throws:
UnsupportedOperationException
-
clear
public void clear() -
n
public int n()Returns the number of items in the filter.- Returns:
- number of items in the filter
-
p
public double p()Returns the probability of false positives, fraction between 0 and 1.- Returns:
- probability of false positives, fraction between 0 and 1
-
m
public int m()Returns the number of bits in the filter.- Returns:
- number of bits in the filter
-
k
public int k()Returns the number of hash functions.- Returns:
- number of hash functions
-
bitset
Returns theBitSetholding the internal bloom filter state.- Returns:
BitSetholding the internal bloom filter state
-
merge
Merges this filter withother. While this filter will be contain the merged result,otherwill be unchanged.- Parameters:
other- bloom filter to merge- Throws:
IllegalArgumentException- if other bloom filter is incompatible (n, p, m, and k must be identical on both filters)
-
getBit
protected boolean getBit(int index) -
setBit
protected boolean setBit(int index) -
hashes
Calculateskhashes forelement.- Parameters:
element- element for which hashes are to be calculated.- Returns:
- calculated hashes for
element
-