Package com.graphaware.common.util
Class ReservoirSampler<T>
- java.lang.Object
-
- com.graphaware.common.util.ReservoirSampler<T>
-
- Type Parameters:
T
- type of sampled item.
public class ReservoirSampler<T> extends Object
Sampler that uses the reservoir sampling algorithm, which is useful when the total number of items from which the sample is being taken is unknown in advance.
-
-
Constructor Summary
Constructors Constructor Description ReservoirSampler(int numberOfSamples)
Create a new sampler with a certain reservoir size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Iterable<T>
getSamples()
Get samples collected in the reservoir.boolean
isEmpty()
Is the reservoir empty?void
sample(T item)
Sample an item and store in the reservoir if needed.
-
-
-
Method Detail
-
sample
public void sample(T item)
Sample an item and store in the reservoir if needed.- Parameters:
item
- The item to sample. Must not be null.
-
getSamples
public Iterable<T> getSamples()
Get samples collected in the reservoir.- Returns:
- A list of the samples.
-
isEmpty
public boolean isEmpty()
Is the reservoir empty?- Returns:
- true iff empty.
-
-