stanfordnlp/CoreNLP · error · IllegalArgumentException
n > size of collection
Error message
n > size of collection: ${n}, ${c.size()} What it means
CollectionUtils.sampleWithoutReplacement throws IllegalArgumentException when n exceeds the collection size, because you cannot draw more distinct elements (without replacement) than the collection contains. This is a hard precondition of the method's contract.
Solutions
- Clamp n to the collection size before calling: Math.min(n, c.size()).
- Guard with an explicit check and choose a fallback (return all elements, or use sampleWithReplacement which allows repeats).
- Validate that the source collection has at least n elements before sampling; skip or rescale otherwise.
- If duplicates are acceptable, switch to sampleWithReplacement(c, n, r).
Example fix
// before Collection<E> s = CollectionUtils.sampleWithoutReplacement(c, 100, r); // after Collection<E> s = CollectionUtils.sampleWithoutReplacement(c, Math.min(100, c.size()), r);
Defensive patterns
Strategy: validation
Validate before calling
if (n > c.size()) throw new IllegalArgumentException("cannot sample " + n + " without replacement from " + c.size()); Type guard
boolean canSampleWithoutReplacement(int n, Collection<?> c) { return n >= 0 && n <= c.size(); } Try / catch
try { return CollectionUtils.sampleWithoutReplacement(c, n, r); } catch (IllegalArgumentException e) { return new ArrayList<>(c); } Prevention
- Always Math.min(n, c.size()) before sampling without replacement
- Recompute n after filtering the source collection
- Use sampleWithReplacement when n may legitimately exceed the size
- Handle empty collections explicitly
When it happens
Trigger: Calling sampleWithoutReplacement(c, n, r) with n > c.size(), e.g. sampling a fixed count from a possibly smaller collection or an empty collection with n > 0.
Common situations: Hardcoded sample sizes applied to small datasets; collections filtered down before sampling without adjusting n; off-by-one (n == c.size() + 1).
Related errors
- n < 0
- Bad arguments: " + x + " and " + lambda
- BiLexPCFGParser doesn't support k sampled parses
- Call next() before calling remove()!
- conditionalLogProbGivenFirst requires of one less than…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/2f1e9e05d1a92f0f.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/CollectionUtils.java:369
*/
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) {
return sampleWithoutReplacement(c, n, new Random());
}
/**
* Samples without replacement from a collection, using your own
* {@link Random} number generator.
*
* @param c The collection to be sampled from
* @param n The number of samples to take
* @param r The random number generator
* @return a new collection with the sample
*/
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {
if (n < 0)
throw new IllegalArgumentException("n < 0: " + n);
if (n > c.size())
throw new IllegalArgumentException("n > size of collection: " + n + ", " + c.size());
List<E> copy = new ArrayList<>(c.size());
copy.addAll(c);
Collection<E> result = new ArrayList<>(n);
for (int k = 0; k < n; k++) {
double d = r.nextDouble();
int x = (int) (d * copy.size());
result.add(copy.remove(x));
}
return result;
}
public static <E> E sample(List<E> l, Random r) {
int i = r.nextInt(l.size());
return l.get(i);
}
/**
* Samples with replacement from a collection.View on GitHub (pinned to 1b7edd19c4)