Class 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 Detail

      • ReservoirSampler

        public ReservoirSampler​(int numberOfSamples)
        Create a new sampler with a certain reservoir size.
        Parameters:
        numberOfSamples - Maximum number of samples to retain in the reservoir. Must be positive.
    • 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.