如果我之前曾致电IllegalStateException
,为什么我在iterator.remove()
中得到iterator.next()
?
有什么可能的原因吗?之前调用过iterator.next()
方法,而没有两次调用过iterator.remove()
。我认为IllegalStateException
没有其他原因。这段代码在Quartz Timer的SimpleThreadPool的工作线程上运行(全部在Tomcat7上运行的Web应用程序中),但这应该无关紧要吗?
请查看下面的代码中大胆注释的部分,以获取更简单的问题概述(来自真实项目)。
public Set<Draw5x36> shortListByFiveDistancePattern(Set<Draw5x36> candidatesToShortlist,
int DEPTH_TO_CHECK_UNIQUENESS_OF_DISTANCE_PATTERN) {
Set<Draw5x36> candidatesToShortlistCopy = new HashSet<>(candidatesToShortlist);
FiveDistancePatternAnalyzer analyzer = new FiveDistancePatternAnalyzer(allDraws);
// GET ITERATOR FROM A SET
Iterator<Draw5x36> candidatesIterator = candidatesToShortlistCopy.iterator();
// CHECK iterator.hasNext() WITHIN LOOP
while (candidatesIterator.hasNext()) {
// CALL iterator.next() - WITHIN METHOD ARGUMENT WITHIN IF CONDITION BELOW
if (!analyzer.isFiveDistancesUniqInNLatestDrawsFromCandidate(candidatesIterator.next(),
DEPTH_TO_CHECK_UNIQUENESS_OF_DISTANCE_PATTERN)) {
// GET IllegalStateException, BUT I CALLED iterator.next() above!
candidatesIterator.remove();
}
}
return candidatesToShortlistCopy;
}
方法参数应在调用方法之前进行求值,如果条件为100%则调用内部方法来评估条件,因此始终iterator.next()
被调用。