oracle/graal · error · UnsupportedOperationException
Length of target array must equal the size of the set.
Error message
Length of target array must equal the size of the set.
What it means
UnmodifiableEconomicSet.toArray(E[] target) requires the target array length to exactly equal set.size(); otherwise it throws UnsupportedOperationException. Unlike java.util.Set.toArray(T[]) it does not allocate, grow, pad with null, or return a different array — it is a strict copy into a caller-provided, correctly sized array. (EconomicMapImpl's EconomicSet view shares this contract.)
Source
Thrown at sdk/src/org.graalvm.collections/src/org/graalvm/collections/UnmodifiableEconomicSet.java:92
* Returns {@code true} if this set contains no elements.
*
* @since 19.0
*/
boolean isEmpty();
/**
* Stores all the elements in this set into {@code target}. An
* {@link UnsupportedOperationException} will be thrown if the length of {@code target} does not
* match the size of this set.
*
* @return an array containing all the elements in this set.
* @throws UnsupportedOperationException if the length of {@code target} does not equal the size
* of this set.
* @since 19.0
*/
default E[] toArray(E[] target) {
if (target.length != size()) {
throw new UnsupportedOperationException("Length of target array must equal the size of the set.");
}
int index = 0;
for (E element : this) {
target[index++] = element;
}
return target;
}
default HashSet<E> toHashSet() {
HashSet<E> set = new HashSet<>(size());
for (E elem : this) {
set.add(elem);
}
return set;
}
View on GitHub (pinned to a66e9ccd1d)
Solutions
- Allocate the target in the same statement as the copy: E[] a = set.toArray(set.newArray(size())) or T[] target = Arrays.copyOf(template, set.size()); then set.toArray(target).
- Snapshot the size once and ensure no mutations occur between sizing and copying (synchronize or copy under read lock).
- Prefer iterating the set directly or toHashSet()/stream collectors instead of toArray when sizing is hard to guarantee.
Example fix
// before E[] arr = set.toArray(new E[0]); // java.util idiom -> throws // after E[] arr = set.toArray(Arrays.copyOf(template, set.size()));
Defensive patterns
Strategy: validation
Validate before calling
E[] target = Arrays.copyOf(template, set.size()); // size snapshot & alloc together E[] result = set.toArray(target);
Prevention
- Allocate the target from the same size() call you copy with; never reuse arrays across sets.
- Do not carry over java.util toArray(new T[0]) idioms.
- Ensure no mutation can occur between sizing and copying; synchronize if concurrent.
When it happens
Trigger: Calling toArray(target) where target was allocated with new E[0], with a cached/stale size, or where the set changed size between the size() call and toArray(); reusing one target array across differently sized sets.
Common situations: Porting java.util.Collection.toArray(T[]) idioms (which tolerate undersized/oversized arrays) to EconomicSet; concurrent modification between sizing and copying; using the classic 'pass empty array as hint' pattern (toArray(new E[0])) which is invalid here.
Related errors
- Cannot modify the always-empty set
- Element {} should be a {}, got {}
- Element {} should be an object, got {}
- Expected an array constant, got {}
- map grown too large!
AI-assisted analysis of oracle/graal@a66e9ccd1d (2026-08-14).
Data as JSON: /api/errors/21eb907af2906456.
Report an issue: GitHub.