ChainSampler implements ISampleList<Serializable> { public void sample(Serializable item) { int i = (int) (count%n); if (replacements.containsKey(i)) { int replace = replacements.get(i); items[replace] = item; int next = rand.nextInt(n); LOG.info(String.format("Item=%s; i=%d; b=%d; next=%d", item, i, replace, next)); replacements.remove(i); replacements.put(next, replace); } else if (fillSample < k) { double prob = ((double)Math.min(i, n))/((double)n); if (rand.nextDouble() < prob) { int bucket = fillSample++; int next = rand.nextInt(n); items[bucket] = item; replacements.put(next, bucket); LOG.info(String.format("[init] Item=%s; i=%d; b=%d; next=%d", item, i, bucket, next)); } } count++; } ChainSampler(int k, int n); void sample(Serializable item); void sample(Serializable... t); Collection<Serializable> getSamples(); int getSize();  }
@Test public void testSample() { System.out.println("sample"); ChainSampler sampler = new ChainSampler(10, 125); for (int j=0; j<1000; j++) { sampler.sample(j); } }