Top Description Inners Fields Constructors Methods
com.google.common.hash

public final Class BloomFilter<T extends @Nullable Object>

extends Object
implements Predicate<T extends @Nullable Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Predicate, java.util.function.Predicate
Annotations
@Beta
@ElementTypesAreNonnullByDefault
Type Parameters
<T>
the type of instances that the BloomFilter accepts
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull

A Bloom filter for instances of 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.

Authors
Dimitris Andreou, Kevin Bourrillion
Since
11.0 (thread-safe since 23.0)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static interface
BloomFilter.Strategy

A strategy to translate T instances, to numHashFunctions bit indexes.

Field Summary

Modifier and TypeField and Description
private final BloomFilterStrategies.LockFreeBitArray
bits

The bit set of the BloomFilter (not necessarily power of 2!)

private final Funnel<? super T>
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
strategy

The strategy we employ to map an element T to numHashFunctions bit indexes.

Constructor Summary

AccessConstructor and Description
private
BloomFilter(BloomFilterStrategies.LockFreeBitArray bits, int numHashFunctions, Funnel<? super T> funnel, BloomFilter.Strategy strategy)

Creates a BloomFilter.

Method Summary

Modifier and TypeMethod and Description
public boolean
apply(T input)

Implements com.google.common.base.Predicate.apply.

Deprecated Provided only to satisfy the Predicate interface; use mightContain instead.
Returns the result of applying this predicate to 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
bitSize()

Returns the number of bits in the underlying bit array.

public BloomFilter<T>
copy()

Creates a new BloomFilter that's a copy of this instance.

public static <T extends @Nullable Object> BloomFilter<T>

Returns:

a BloomFilter
create
(Funnel<? super T>
the funnel of T's that the constructed BloomFilter will use
funnel
,
int
the number of expected insertions to the constructed BloomFilter; must be positive
expectedInsertions
,
double
the desired false positive probability (must be positive and less than 1.0)
fpp
)

Creates a BloomFilter with the expected number of insertions and expected false positive probability.

public static <T extends @Nullable Object> BloomFilter<T>

Returns:

a BloomFilter
create
(Funnel<? super T>
the funnel of T's that the constructed BloomFilter will use
funnel
,
long
the number of expected insertions to the constructed BloomFilter; must be positive
expectedInsertions
,
double
the desired false positive probability (must be positive and less than 1.0)
fpp
)

Creates a BloomFilter with the expected number of insertions and expected false positive probability.

pack-priv static <T extends @Nullable Object> BloomFilter<T>
create(Funnel<? super T> funnel, long expectedInsertions, double fpp, BloomFilter.Strategy strategy)

public static <T extends @Nullable Object> BloomFilter<T>

Returns:

a BloomFilter
create
(Funnel<? super T>
the funnel of T's that the constructed BloomFilter will use
funnel
,
int
the number of expected insertions to the constructed BloomFilter; must be positive
expectedInsertions
)

Creates a BloomFilter with the expected number of insertions and a default expected false positive probability of 3%.

public static <T extends @Nullable Object> BloomFilter<T>

Returns:

a BloomFilter
create
(Funnel<? super T>
the funnel of T's that the constructed BloomFilter will use
funnel
,
long
the number of expected insertions to the constructed BloomFilter; must be positive
expectedInsertions
)

Creates a BloomFilter with the expected number of insertions and a default expected false positive probability of 3%.

public boolean
equals(Object
the reference object with which to compare.
object
)

Overrides java.lang.Object.equals.

Implements com.google.common.base.Predicate.equals.

Indicates whether another object is equal to this predicate.
public double
expectedFpp()

Returns the probability that mightContain(Object) will erroneously return true for an object that has not actually been put in the BloomFilter.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
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 true if the element might have been put in this Bloom filter, false if this is definitely not the case.

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 time object 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.
put
(T object)

Puts an element into this BloomFilter.

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<T>
readFrom(InputStream in, Funnel<? super T> funnel)

Reads a byte stream, which was written by writeTo(OutputStream), into a BloomFilter.

private void
public static <T extends @Nullable Object> Collector<T, ?, BloomFilter<T>>

Returns:

a Collector generating a BloomFilter of the received elements
toBloomFilter
(Funnel<? super T>
the funnel of T's that the constructed BloomFilter will use
funnel
,
long
the number of expected insertions to the constructed BloomFilter; must be positive
expectedInsertions
)

Returns a Collector expecting the specified number of insertions, and yielding a BloomFilter with false positive probability 3%.

public static <T extends @Nullable Object> Collector<T, ?, BloomFilter<T>>

Returns:

a Collector generating a BloomFilter of the received elements
toBloomFilter
(Funnel<? super T>
the funnel of T's that the constructed BloomFilter will use
funnel
,
long
the number of expected insertions to the constructed BloomFilter; must be positive
expectedInsertions
,
double
the desired false positive probability (must be positive and less than 1.0)
fpp
)

Returns a Collector expecting the specified number of insertions, and yielding a BloomFilter with the specified expected false positive probability.

private Object
public void
writeTo(OutputStream out)

Writes this BloomFilter to an output stream, with a custom format (not Java serialization).

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

bitsback to summary
private final BloomFilterStrategies.LockFreeBitArray bits

The bit set of the BloomFilter (not necessarily power of 2!)

funnelback to summary
private final Funnel<? super T> funnel

The funnel to translate Ts to bytes

numHashFunctionsback to summary
private final int numHashFunctions

Number of hashes per element

serialVersionUIDback to summary
private static final long serialVersionUID
strategyback to summary
private final BloomFilter.Strategy strategy

The strategy we employ to map an element T to numHashFunctions bit indexes.

Constructor Detail

BloomFilterback to summary
private BloomFilter(BloomFilterStrategies.LockFreeBitArray bits, int numHashFunctions, Funnel<? super T> funnel, BloomFilter.Strategy strategy)

Creates a BloomFilter.

Method Detail

applyback to summary
public boolean apply(T input)

Implements com.google.common.base.Predicate.apply.

Doc from com.google.common.base.Predicate.apply.

Deprecated

Provided only to satisfy the Predicate interface; use mightContain instead.

Returns the result of applying this predicate to input (Java 8+ users, see notes in the class documentation above). This method is generally expected, but not absolutely required, to have the following properties:

  • Its execution does not cause any observable side effects.
  • The computation is consistent with equals; that is, Objects.equal(a, b) implies that predicate.apply(a) == predicate.apply(b)).
Annotations
@Deprecated
@Override
approximateElementCountback 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 expectedInsertions that was used when constructing the filter.

Since
22.0
bitSizeback to summary
pack-priv long bitSize()

Returns the number of bits in the underlying bit array.

Annotations
@VisibleForTesting
copyback to summary
public BloomFilter<T> copy()

Creates a new BloomFilter that's a copy of this instance. The new instance is equal to this instance but shares no mutable state.

Since
12.0
createback to summary
public static <T extends @Nullable Object> BloomFilter<T> create(Funnel<? super T> funnel, int expectedInsertions, double fpp)

Creates a BloomFilter with the expected number of insertions and expected false positive probability.

Note that overflowing a BloomFilter with significantly more elements than specified, will result in its saturation, and a sharp deterioration of its false positive probability.

The constructed BloomFilter will be serializable if the provided Funnel<T> is.

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 also relies on object identity of funnels.

Parameters
funnel:Funnel<? super T>

the funnel of T's that the constructed BloomFilter will use

expectedInsertions:int

the number of expected insertions to the constructed BloomFilter; must be positive

fpp:double

the desired false positive probability (must be positive and less than 1.0)

Returns:BloomFilter<T>

a BloomFilter

createback to summary
public static <T extends @Nullable Object> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp)

Creates a BloomFilter with the expected number of insertions and expected false positive probability.

Note that overflowing a BloomFilter with significantly more elements than specified, will result in its saturation, and a sharp deterioration of its false positive probability.

The constructed BloomFilter will be serializable if the provided Funnel<T> is.

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 also relies on object identity of funnels.

Parameters
funnel:Funnel<? super T>

the funnel of T's that the constructed BloomFilter will use

expectedInsertions:long

the number of expected insertions to the constructed BloomFilter; must be positive

fpp:double

the desired false positive probability (must be positive and less than 1.0)

Returns:BloomFilter<T>

a BloomFilter

Since
19.0
createback to summary
pack-priv static <T extends @Nullable Object> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp, BloomFilter.Strategy strategy)
Annotations
@VisibleForTesting
createback to summary
public static <T extends @Nullable Object> BloomFilter<T> create(Funnel<? super T> funnel, int expectedInsertions)

Creates a BloomFilter with the expected number of insertions and a default expected false positive probability of 3%.

Note that overflowing a BloomFilter with significantly more elements than specified, will result in its saturation, and a sharp deterioration of its false positive probability.

The constructed BloomFilter will be serializable if the provided Funnel<T> is.

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 also relies on object identity of funnels.

Parameters
funnel:Funnel<? super T>

the funnel of T's that the constructed BloomFilter will use

expectedInsertions:int

the number of expected insertions to the constructed BloomFilter; must be positive

Returns:BloomFilter<T>

a BloomFilter

createback to summary
public static <T extends @Nullable Object> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions)

Creates a BloomFilter with the expected number of insertions and a default expected false positive probability of 3%.

Note that overflowing a BloomFilter with significantly more elements than specified, will result in its saturation, and a sharp deterioration of its false positive probability.

The constructed BloomFilter will be serializable if the provided Funnel<T> is.

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 also relies on object identity of funnels.

Parameters
funnel:Funnel<? super T>

the funnel of T's that the constructed BloomFilter will use

expectedInsertions:long

the number of expected insertions to the constructed BloomFilter; must be positive

Returns:BloomFilter<T>

a BloomFilter

Since
19.0
equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Implements com.google.common.base.Predicate.equals.

Doc from com.google.common.base.Predicate.equals.

Indicates whether another object is equal to this predicate.

Most implementations will have no reason to override the behavior of Object#equals. However, an implementation may also choose to return true whenever object is a Predicate that it considers interchangeable with this one. "Interchangeable" typically means that this.apply(t) == that.apply(t) for all t of type T). Note that a false result from this method does not imply that the predicates are known not to be interchangeable.

Parameters
object:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
expectedFppback to summary
public double expectedFpp()

Returns the probability that mightContain(Object) will erroneously return true for an object that has not actually been put in the BloomFilter.

Ideally, this number should be close to the fpp parameter passed in create(Funnel, int, double), or smaller. If it is significantly higher, it is usually the case that too many elements (more than expected) have been put in the BloomFilter, degenerating it.

Since
14.0 (since 11.0 as expectedFalsePositiveProbability())
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isCompatibleback 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:

  • not be the same instance
  • have the same number of hash functions
  • have the same bit size
  • have the same strategy
  • have equal funnels
Parameters
that:BloomFilter<T>

The Bloom filter to check for compatibility.

Since
15.0
mightContainback to summary
public boolean mightContain(T object)

Returns true if the element might have been put in this Bloom filter, false if this is definitely not the case.

optimalNumOfBitsback 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.

Parameters
n:long

expected insertions (must be positive)

p:double

false positive rate (must be 0 < p < 1)

Annotations
@VisibleForTesting
optimalNumOfHashFunctionsback 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.

Parameters
n:long

expected insertions (must be positive)

m:long

total number of bits in Bloom filter (must be positive)

Annotations
@VisibleForTesting
putback to summary
public boolean put(T object)

Puts an element into this BloomFilter. Ensures that subsequent invocations of mightContain(Object) with the same element will always return true.

Returns:boolean

true if the Bloom filter's bits changed as a result of this operation. If the bits changed, this is definitely the first time object 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.

Annotations
@CanIgnoreReturnValue
Since
12.0 (present in 11.0 with void return type})
putAllback 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.

Parameters
that:BloomFilter<T>

The Bloom filter to combine this Bloom filter with. It is not mutated.

Exceptions
IllegalArgumentException:
if isCompatible(that) == false
Since
15.0
readFromback to summary
public static <T extends @Nullable Object> BloomFilter<T> readFrom(InputStream in, Funnel<? super T> funnel) throws IOException

Reads a byte stream, which was written by writeTo(OutputStream), into a BloomFilter.

The Funnel to be used is not encoded in the stream, so it must be provided here

Warning

the funnel provided must behave identically to the one used to populate the original Bloom filter!

Annotations
@SuppressWarnings:CatchingUnchecked
Exceptions
IOException:
if the InputStream throws an IOException, or if its data does not appear to be a BloomFilter serialized using the writeTo(OutputStream) method.
readObjectback to summary
private void readObject(ObjectInputStream stream) throws InvalidObjectException
toBloomFilterback to summary
public static <T extends @Nullable Object> Collector<T, ?, BloomFilter<T>> toBloomFilter(Funnel<? super T> funnel, long expectedInsertions)

Returns a Collector expecting the specified number of insertions, and yielding a BloomFilter with false positive probability 3%.

Note that if the Collector receives significantly more elements than specified, the resulting BloomFilter will suffer a sharp deterioration of its false positive probability.

The constructed BloomFilter will be serializable if the provided Funnel<T> is.

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 also relies on object identity of funnels.

Parameters
funnel:Funnel<? super T>

the funnel of T's that the constructed BloomFilter will use

expectedInsertions:long

the number of expected insertions to the constructed BloomFilter; must be positive

Returns:Collector<T, ?, BloomFilter<T>>

a Collector generating a BloomFilter of the received elements

Since
23.0
toBloomFilterback to summary
public static <T extends @Nullable Object> Collector<T, ?, BloomFilter<T>> toBloomFilter(Funnel<? super T> funnel, long expectedInsertions, double fpp)

Returns a Collector expecting the specified number of insertions, and yielding a BloomFilter with the specified expected false positive probability.

Note that if the Collector receives significantly more elements than specified, the resulting BloomFilter will suffer a sharp deterioration of its false positive probability.

The constructed BloomFilter will be serializable if the provided Funnel<T> is.

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 also relies on object identity of funnels.

Parameters
funnel:Funnel<? super T>

the funnel of T's that the constructed BloomFilter will use

expectedInsertions:long

the number of expected insertions to the constructed BloomFilter; must be positive

fpp:double

the desired false positive probability (must be positive and less than 1.0)

Returns:Collector<T, ?, BloomFilter<T>>

a Collector generating a BloomFilter of the received elements

Since
23.0
writeReplaceback to summary
private Object writeReplace()
writeToback to summary
public void writeTo(OutputStream out) throws IOException

Writes this BloomFilter to an output stream, with a custom format (not Java serialization). This has been measured to save at least 400 bytes compared to regular serialization.

Use readFrom(InputStream, Funnel) to reconstruct the written BloomFilter.

com.google.common.hash back to summary

private Class BloomFilter.SerialForm<T extends @Nullable Object>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Field Summary

Modifier and TypeField and Description
pack-priv final long[]
pack-priv final Funnel<? super T>
pack-priv final int
private static final long
pack-priv final BloomFilter.Strategy

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databack to summary
pack-priv final long[] data
funnelback to summary
pack-priv final Funnel<? super T> funnel
numHashFunctionsback to summary
pack-priv final int numHashFunctions
serialVersionUIDback to summary
private static final long serialVersionUID
strategyback to summary
pack-priv final BloomFilter.Strategy strategy

Constructor Detail

SerialFormback to summary
pack-priv SerialForm(BloomFilter<T> bf)

Method Detail

readResolveback to summary
pack-priv Object readResolve()
com.google.common.hash back to summary

pack-priv Interface BloomFilter.Strategy

extends Serializable
Known Direct Implementers
com.google.common.hash.BloomFilterStrategies

A strategy to translate T instances, to numHashFunctions bit indexes.

Implementations should be collections of pure functions (i.e. stateless).

Method Summary

Modifier and TypeMethod and Description
public <T extends @Nullable Object> boolean
mightContain(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.LockFreeBitArray bits)

Queries numHashFunctions bits of the given bit array, by hashing a user element; returns true if and only if all selected bits are set.

public int
ordinal()

Identifier used to encode this strategy, when marshalled as part of a BloomFilter.

public <T extends @Nullable Object> boolean
put(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.LockFreeBitArray bits)

Sets numHashFunctions bits of the given bit array, by hashing a user element.

Method Detail

mightContainback to summary
public <T extends @Nullable Object> boolean mightContain(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.LockFreeBitArray bits)

Queries numHashFunctions bits of the given bit array, by hashing a user element; returns true if and only if all selected bits are set.

ordinalback 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).

putback to summary
public <T extends @Nullable Object> boolean put(T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.LockFreeBitArray bits)

Sets numHashFunctions bits of the given bit array, by hashing a user element.

Returns whether any bits changed as a result of this operation.