BloomFilter accepts
T. A Bloom filter offers an approximate containment test
with one-sided error: if it claims that an element is contained in it, this might be in error,
but if it claims that an element is not contained in it, then this is definitely true.
If you are unfamiliar with Bloom filters, this nice tutorial may help you understand how they work.
The false positive probability (FPP) of a Bloom filter is defined as the probability
that mightContain(Object) will erroneously return true for an object that
has not actually been put in the BloomFilter.
Bloom filters are serializable. They also support a more compact serial representation via the
writeTo and readFrom methods. Both serialized forms will continue to be
supported by future versions of this library. However, serial forms generated by newer versions
of the code may not be readable by older versions of the code (e.g., a serialized Bloom filter
generated today may not be readable by a binary that was compiled 6 months ago).
As of Guava 23.0, this class is thread-safe and lock-free. It internally uses atomics and compare-and-swap to ensure correctness when multiple threads are used to access it.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| pack-priv static interface | BloomFilter.
A strategy to translate T instances, to |
| Modifier and Type | Field and Description |
|---|---|
| private final BloomFilterStrategies. | bits
The bit set of the BloomFilter (not necessarily power of 2!) |
| private final Funnel | funnel
The funnel to translate Ts to bytes |
| private final int | numHashFunctions
Number of hashes per element |
| private static final long | |
| private final BloomFilter. | strategy
The strategy we employ to map an element T to |
| Access | Constructor and Description |
|---|---|
| private | BloomFilter(BloomFilterStrategies.
Creates a BloomFilter. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | apply(T input)
Implements com.
Deprecated
Provided only to satisfy the
Returns the result of applying this predicate to Predicate interface; use mightContain
instead.
input (Java 8+ users, see notes in the
class documentation above).
|
| public long | approximateElementCount()
Returns an estimate for the total number of distinct elements that have been added to this Bloom filter. |
| pack-priv long | |
| public BloomFilter | |
| public static <T extends @Nullable Object> BloomFilter | Returns: aBloomFilterthe funnel of T's that the constructed funnel, int BloomFilter will usethe number of expected insertions to the constructed expectedInsertions, double BloomFilter; must be positivethe desired false positive probability (must be positive and less than 1.0) fpp)Creates a |
| public static <T extends @Nullable Object> BloomFilter | Returns: aBloomFilterthe funnel of T's that the constructed funnel, long BloomFilter will usethe number of expected insertions to the constructed expectedInsertions, double BloomFilter; must be positivethe desired false positive probability (must be positive and less than 1.0) fpp)Creates a |
| pack-priv static <T extends @Nullable Object> BloomFilter | create(Funnel<? super T> funnel, long expectedInsertions, double fpp, BloomFilter.
|
| public static <T extends @Nullable Object> BloomFilter | Returns: aBloomFilterthe funnel of T's that the constructed funnel, int BloomFilter will usethe number of expected insertions to the constructed expectedInsertions)BloomFilter; must be positiveCreates a |
| public static <T extends @Nullable Object> BloomFilter | Returns: aBloomFilterthe funnel of T's that the constructed funnel, long BloomFilter will usethe number of expected insertions to the constructed expectedInsertions)BloomFilter; must be positiveCreates a |
| public boolean | equals(Object
the reference object with which to compare. object)Overrides java. Implements com. |
| public double | expectedFpp()
Returns the probability that mightContain(Object) will erroneously return |
| public int | |
| public boolean | isCompatible(BloomFilter<T>
The Bloom filter to check for compatibility. that)Determines whether a given Bloom filter is compatible with this Bloom filter. |
| public boolean | mightContain(T object)
Returns |
| pack-priv static long | optimalNumOfBits(long
expected insertions (must be positive) n, double false positive rate (must be 0 < p < 1) p)Computes m (total bits of Bloom filter) which is expected to achieve, for the specified expected insertions, the required false positive probability. |
| pack-priv static int | optimalNumOfHashFunctions(long
expected insertions (must be positive) n, long total number of bits in Bloom filter (must be positive) m)Computes the optimal k (number of hashes per element inserted in Bloom filter), given the expected insertions and total number of bits in the Bloom filter. |
| public boolean | Returns: true if the Bloom filter's bits changed as a result of this operation. If the bits changed, this is definitely the first timeobject has been added to the
filter. If the bits haven't changed, this might be the first time object has
been added to the filter. Note that put(t) always returns the opposite
result to what mightContain(t) would have returned at the time it is called.Puts an element into this |
| public void | putAll(BloomFilter<T>
The Bloom filter to combine this Bloom filter with. It is not mutated. that)Combines this Bloom filter with another Bloom filter by performing a bitwise OR of the underlying data. |
| public static <T extends @Nullable Object> BloomFilter | readFrom(InputStream in, Funnel<? super T> funnel)
Reads a byte stream, which was written by writeTo(OutputStream), into a |
| private void | |
| public static <T extends @Nullable Object> Collector | Returns: aCollector generating a BloomFilter of the received elementsthe funnel of T's that the constructed funnel, long BloomFilter will usethe number of expected insertions to the constructed expectedInsertions)BloomFilter; must be positiveReturns a |
| public static <T extends @Nullable Object> Collector | Returns: aCollector generating a BloomFilter of the received elementsthe funnel of T's that the constructed funnel, long BloomFilter will usethe number of expected insertions to the constructed expectedInsertions, double BloomFilter; must be positivethe desired false positive probability (must be positive and less than 1.0) fpp)Returns a |
| private Object | |
| public void | writeTo(OutputStream out)
Writes this |
| bits | back to summary |
|---|---|
| private final BloomFilterStrategies. The bit set of the BloomFilter (not necessarily power of 2!) | |
| funnel | back to summary |
|---|---|
| private final Funnel<? super T> funnel The funnel to translate Ts to bytes | |
| numHashFunctions | back to summary |
|---|---|
| private final int numHashFunctions Number of hashes per element | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| strategy | back to summary |
|---|---|
| private final BloomFilter. The strategy we employ to map an element T to | |
| BloomFilter | back to summary |
|---|---|
| private BloomFilter(BloomFilterStrategies. Creates a BloomFilter. | |
| apply | back to summary |
|---|---|
| public boolean apply(T input) Implements com. Doc from com. Deprecated Provided only to satisfy the Returns the result of applying this predicate to
| |
| approximateElementCount | back to summary |
|---|---|
| public long approximateElementCount() Returns an estimate for the total number of distinct elements that have been added to this
Bloom filter. This approximation is reasonably accurate if it does not exceed the value of
| |
| bitSize | back to summary |
|---|---|
| pack-priv long bitSize() Returns the number of bits in the underlying bit array.
| |
| copy | back to summary |
|---|---|
| public BloomFilter Creates a new
| |
| create | back to summary |
|---|---|
| public static <T extends @Nullable Object> BloomFilter Creates a Note that overflowing a The constructed It is recommended that the funnel be implemented as a Java enum. This has the benefit of
ensuring proper serialization and deserialization, which is important since
| |
| create | back to summary |
|---|---|
| public static <T extends @Nullable Object> BloomFilter Creates a Note that overflowing a The constructed It is recommended that the funnel be implemented as a Java enum. This has the benefit of
ensuring proper serialization and deserialization, which is important since
| |
| create | back to summary |
|---|---|
pack-priv static <T extends @Nullable Object> BloomFilter
| |
| create | back to summary |
|---|---|
| public static <T extends @Nullable Object> BloomFilter Creates a Note that overflowing a The constructed It is recommended that the funnel be implemented as a Java enum. This has the benefit of
ensuring proper serialization and deserialization, which is important since
| |
| create | back to summary |
|---|---|
| public static <T extends @Nullable Object> BloomFilter Creates a Note that overflowing a The constructed It is recommended that the funnel be implemented as a Java enum. This has the benefit of
ensuring proper serialization and deserialization, which is important since
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Overrides java. Implements com. Doc from com. Indicates whether another object is equal to this predicate. Most implementations will have no reason to override the behavior of
| |
| expectedFpp | back to summary |
|---|---|
| public double expectedFpp() Returns the probability that mightContain(Object) will erroneously return Ideally, this number should be close to the
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isCompatible | back to summary |
|---|---|
| public boolean isCompatible(BloomFilter<T> that) Determines whether a given Bloom filter is compatible with this Bloom filter. For two Bloom filters to be compatible, they must:
| |
| mightContain | back to summary |
|---|---|
| public boolean mightContain(T object) Returns | |
| optimalNumOfBits | back to summary |
|---|---|
| pack-priv static long optimalNumOfBits(long n, double p) Computes m (total bits of Bloom filter) which is expected to achieve, for the specified expected insertions, the required false positive probability. See http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives for the formula.
| |
| optimalNumOfHashFunctions | back to summary |
|---|---|
| pack-priv static int optimalNumOfHashFunctions(long n, long m) Computes the optimal k (number of hashes per element inserted in Bloom filter), given the expected insertions and total number of bits in the Bloom filter. See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula.
| |
| put | back to summary |
|---|---|
| public boolean put(T object) Puts an element into this
| |
| putAll | back to summary |
|---|---|
| public void putAll(BloomFilter<T> that) Combines this Bloom filter with another Bloom filter by performing a bitwise OR of the underlying data. The mutations happen to this instance. Callers must ensure the Bloom filters are appropriately sized to avoid saturating them.
| |
| readFrom | back to summary |
|---|---|
| public static <T extends @Nullable Object> BloomFilter Reads a byte stream, which was written by writeTo(OutputStream), into a The Warning the funnel provided must behave identically to the one used to populate the original Bloom filter!
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream stream) throws InvalidObjectException | |
| toBloomFilter | back to summary |
|---|---|
| public static <T extends @Nullable Object> Collector Returns a Note that if the The constructed It is recommended that the funnel be implemented as a Java enum. This has the benefit of
ensuring proper serialization and deserialization, which is important since
| |
| toBloomFilter | back to summary |
|---|---|
| public static <T extends @Nullable Object> Collector Returns a Note that if the The constructed It is recommended that the funnel be implemented as a Java enum. This has the benefit of
ensuring proper serialization and deserialization, which is important since
| |
| writeReplace | back to summary |
|---|---|
| private Object writeReplace() | |
| writeTo | back to summary |
|---|---|
| public void writeTo(OutputStream out) throws IOException Writes this Use readFrom(InputStream, Funnel) to reconstruct the written BloomFilter. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final long[] | |
| pack-priv final Funnel | |
| pack-priv final int | |
| private static final long | |
| pack-priv final BloomFilter. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv Object |
| data | back to summary |
|---|---|
| pack-priv final long[] data | |
| funnel | back to summary |
|---|---|
| pack-priv final Funnel<? super T> funnel | |
| numHashFunctions | back to summary |
|---|---|
| pack-priv final int numHashFunctions | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| strategy | back to summary |
|---|---|
| pack-priv final BloomFilter. | |
| SerialForm | back to summary |
|---|---|
| pack-priv SerialForm(BloomFilter<T> bf) | |
| readResolve | back to summary |
|---|---|
| pack-priv Object readResolve() | |
numHashFunctions bit indexes.
Implementations should be collections of pure functions (i.e. stateless).
| Modifier and Type | Method and Description |
|---|---|
| public <T extends @Nullable Object> boolean | mightContain(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.
Queries |
| public int | |
| public <T extends @Nullable Object> boolean | put(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.
Sets |
| mightContain | back to summary |
|---|---|
| public <T extends @Nullable Object> boolean mightContain(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies. Queries | |
| ordinal | back to summary |
|---|---|
| public int ordinal() Identifier used to encode this strategy, when marshalled as part of a BloomFilter. Only values in the [-128, 127] range are valid for the compact serial form. Non-negative values are reserved for enums defined in BloomFilterStrategies; negative values are reserved for any custom, stateful strategy we may define (e.g. any kind of strategy that would depend on user input). | |
| put | back to summary |
|---|---|
| public <T extends @Nullable Object> boolean put(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies. Sets Returns whether any bits changed as a result of this operation. | |