chinabugotech/hutool · warning · IllegalArgumentException
Duplicate iterator
Error message
Duplicate iterator
What it means
Thrown by IterChain.addChain(Iterator<T> iterator) when the exact same Iterator instance (by reference equality) is added to the chain more than once. IterChain uses a List.contains() check to prevent duplicate iterator references, which protects against the same iterator being consumed twice during chained traversal.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/collection/IterChain.java:42
*/
public IterChain() {
}
/**
* 构造
* @param iterators 多个{@link Iterator}
*/
@SafeVarargs
public IterChain(Iterator<T>... iterators) {
for (final Iterator<T> iterator : iterators) {
addChain(iterator);
}
}
@Override
public IterChain<T> addChain(Iterator<T> iterator) {
if (allIterators.contains(iterator)) {
throw new IllegalArgumentException("Duplicate iterator");
}
allIterators.add(iterator);
return this;
}
// ---------------------------------------------------------------- interface
protected int currentIter = -1;
@Override
public boolean hasNext() {
if (currentIter == -1) {
currentIter = 0;
}
final int size = allIterators.size();
for (int i = currentIter; i < size; i++) {
final Iterator<T> iterator = allIterators.get(i);View on GitHub (pinned to 8870454b2a)
Solutions
- Ensure each Iterator added to the chain is a distinct instance.
- Call collection.iterator() separately for each position if you need to iterate the same source multiple times.
- Deduplicate iterator references before adding them to the chain.
Example fix
// before Iterator<String> iter = list.iterator(); IterChain<String> chain = new IterChain<>(iter, iter); // throws Duplicate iterator // after IterChain<String> chain = new IterChain<>(list.iterator(), list.iterator()); // distinct instances
Defensive patterns
Strategy: validation
Validate before calling
// Deduplicate iterators before adding to chain
Set<Iterator<T>> seen = new HashSet<>();
for (Iterator<T> iter : iterators) {
if (!seen.contains(iter)) {
chain.addChain(iter);
seen.add(iter);
}
} Prevention
- Call collection.iterator() separately for each chain position.
- Deduplicate iterator references before adding to IterChain.
- Avoid reusing the same iterator variable across addChain() calls.
When it happens
Trigger: Calling iterChain.addChain(sameIterator) twice, or constructing new IterChain(iter1, iter1) with the same iterator reference passed multiple times. The varargs constructor calls addChain for each argument, so passing the same reference twice in the constructor also triggers it.
Common situations: Programmatically building an IterChain in a loop where the same iterator reference is accidentally added twice. Refactoring code that collects iterators into a list without deduplication. Passing the same collection.iterator() result in multiple positions.
Related errors
- remove() method not supported for a NodeListIterator.
- Cache values Iterator is not support to modify.
- [{}] is not support to get empty!
- Unsupported object type: {className}
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/988e62078d0f1c06.
Report an issue: GitHub.