{"record":{"id":"2f1e9e05d1a92f0f","repo":"stanfordnlp/CoreNLP","slug":"n-size-of-collection-n-c-size","errorCode":null,"errorMessage":"n > size of collection: ${n}, ${c.size()}","messagePattern":"n > size of collection: (.+?), (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/CollectionUtils.java","lineNumber":369,"sourceCode":"   */\n  public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) {\n    return sampleWithoutReplacement(c, n, new Random());\n  }\n\n  /**\n   * Samples without replacement from a collection, using your own\n   * {@link Random} number generator.\n   *\n   * @param c The collection to be sampled from\n   * @param n The number of samples to take\n   * @param r The random number generator\n   * @return a new collection with the sample\n   */\n  public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {\n    if (n < 0)\n      throw new IllegalArgumentException(\"n < 0: \" + n);\n    if (n > c.size())\n      throw new IllegalArgumentException(\"n > size of collection: \" + n + \", \" + c.size());\n    List<E> copy = new ArrayList<>(c.size());\n    copy.addAll(c);\n    Collection<E> result = new ArrayList<>(n);\n    for (int k = 0; k < n; k++) {\n      double d = r.nextDouble();\n      int x = (int) (d * copy.size());\n      result.add(copy.remove(x));\n    }\n    return result;\n  }\n\n  public static <E> E sample(List<E> l, Random r) {\n    int i = r.nextInt(l.size());\n    return l.get(i);\n  }\n\n  /**\n   * Samples with replacement from a collection.","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/CollectionUtils.java#L351-L387","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hardcoded sample sizes applied to small datasets; collections filtered down before sampling without adjusting n; off-by-one (n == c.size() + 1).","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)."],"exampleFix":"// before\nCollection<E> s = CollectionUtils.sampleWithoutReplacement(c, 100, r);\n// after\nCollection<E> s = CollectionUtils.sampleWithoutReplacement(c, Math.min(100, c.size()), r);","handlingStrategy":"validation","validationCode":"if (n > c.size()) throw new IllegalArgumentException(\"cannot sample \" + n + \" without replacement from \" + c.size());","typeGuard":"boolean canSampleWithoutReplacement(int n, Collection<?> c) { return n >= 0 && n <= c.size(); }","tryCatchPattern":"try { return CollectionUtils.sampleWithoutReplacement(c, n, r); } catch (IllegalArgumentException e) { return new ArrayList<>(c); }","preventionTips":["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"],"tags":["java","collections","sampling","argument-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}