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
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionBloomFilter
(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.protected
BloomFilter
(BloomFilter.Parameters parameters, Function<E, byte[]> bytesSupplier) protected
BloomFilter
(BloomFilter.Parameters parameters, Function<E, byte[]> bytesSupplier, BitSet bitSet) -
Method Summary
Modifier and TypeMethodDescriptionboolean
Addselement
to the filter.boolean
addAll
(Collection<? extends E> c) bitset()
Returns theBitSet
holding the internal bloom filter state.void
clear()
boolean
Checks ifo
is contained in the filter.boolean
containsAll
(Collection<?> c) protected boolean
getBit
(int index) protected int[]
Calculatesk
hashes forelement
.boolean
isEmpty()
iterator()
Not supported.int
k()
Returns the number of hash functions.int
m()
Returns the number of bits in the filter.void
merge
(BloomFilter<E> other) Merges this filter withother
.int
n()
Returns the number of items in the filter.double
p()
Returns the probability of false positives, fraction between 0 and 1.boolean
Not supported.boolean
removeAll
(Collection<?> c) Not supported.boolean
retainAll
(Collection<?> c) Not supported.protected boolean
setBit
(int index) int
size()
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, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods 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 parameter0
as it must be derived from the other parameters.Visit Bloom Filter Calculator to get more information about the implications/calculation of these parameters.
If
m
is 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
, andk
p
- Probability of false positives, fraction between 0 and 1. If0
, this value is derived fromn
,m
, andk
m
- Number of bits in the filter. If0
, this value is derived fromn
andp
k
- Number of hash functions. If0
, this value is derived fromm
andn
bytesSupplier
- aFunction
that convert objects of typeE
tobyte[]
arraysbitSet
-BitSet
holding this bloom filter's state. Caller must ensure that it has the correct size.- Throws:
IllegalArgumentException
- if one parameter is not0
orp
is not between 0 and 1NullPointerException
- ifbytesSupplier
isnull
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0
as it must be derived from the other parameters.Visit Bloom Filter Calculator to get more information about the implications/calculation of these parameters.
If
m
is 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
, andk
p
- Probability of false positives, fraction between 0 and 1. If0
, this value is derived fromn
,m
, andk
m
- Number of bits in the filter. If0
, this value is derived fromn
andp
k
- Number of hash functions. If0
, this value is derived fromm
andn
bytesSupplier
- aFunction
that convert objects of typeE
tobyte[]
arrays- Throws:
IllegalArgumentException
- if one parameter is not0
orp
is not between 0 and 1NullPointerException
- ifbytesSupplier
isnull
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0
as 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
- aFunction
that convert objects of typeE
tobyte[]
arraysbitSet
-BitSet
holding this bloom filter's state. Caller must ensure that it has the correct size.- Throws:
IllegalArgumentException
- ifn
orp
is0
orp
is not between 0 and 1NullPointerException
- ifbytesSupplier
isnull
-
BloomFilter
Create a new bloom filter. Make sure to leave one parameter0
as 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
- aFunction
that convert objects of typeE
tobyte[]
arrays- Throws:
IllegalArgumentException
- ifn
orp
is0
orp
is not between 0 and 1NullPointerException
- ifbytesSupplier
isnull
-
-
Method Details
-
toString
-
size
public int size()Not supported. ThrowsUnsupportedOperationException
.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Returns:
- throws
UnsupportedOperationException
- Throws:
UnsupportedOperationException
-
isEmpty
public boolean isEmpty() -
contains
Checks ifo
is contained in the filter. False positive messages may occur. -
iterator
Not supported. ThrowsUnsupportedOperationException
.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceSet<E>
- Returns:
- throws
UnsupportedOperationException
- Throws:
UnsupportedOperationException
-
toArray
Not supported. ThrowsUnsupportedOperationException
.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Returns:
- throws
UnsupportedOperationException
- Throws:
UnsupportedOperationException
-
toArray
public <T> T[] toArray(T[] a) Not supported. ThrowsUnsupportedOperationException
.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Returns:
- throws
UnsupportedOperationException
- Throws:
UnsupportedOperationException
-
add
Addselement
to the filter. -
remove
Not supported. ThrowsUnsupportedOperationException
.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Returns:
- throws
UnsupportedOperationException
- Throws:
UnsupportedOperationException
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
-
addAll
-
retainAll
Not supported. ThrowsUnsupportedOperationException
.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceSet<E>
- Returns:
- throws
UnsupportedOperationException
- Throws:
UnsupportedOperationException
-
removeAll
Not supported. ThrowsUnsupportedOperationException
.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in 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 theBitSet
holding the internal bloom filter state.- Returns:
BitSet
holding the internal bloom filter state
-
merge
Merges this filter withother
. While this filter will be contain the merged result,other
will 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
Calculatesk
hashes forelement
.- Parameters:
element
- element for which hashes are to be calculated.- Returns:
- calculated hashes for
element
-