我实现一个Iterator它利用另一个Iterator,它的我不知道,它是否支持remove()
方法还是不行。Java异常Iterator的合同删除
考虑下面的边缘情况:底层迭代器不支持remove()
和我的迭代器next()
尚未被调用。
我是否违反了合同的接口,如果我的remove()
在这种情况下–的IllegalStateException
而不是UnsupportedOperationException
抛出–?
(只要next()
被调用,底层remove()
可以调用,这将引发相应的UnsupportedOperationException
。)
如果是这样,我怎么能修改我的代码来检查底层迭代器是否支持remove()
与否?
一个例子:
<T> Iterator<T> getSetViewIterator(Collection<T> collection) {
Iterator<T> uniqueItr = new HashSet<>(collection).iterator();
return new Iterator<T>() {
private T current = null;
private boolean hasRemoved = true;
@Override
public boolean hasNext() {
return uniqueItr.hasNext();
}
@Override
public T next() {
if(!hasNext())
throw new NoSuchElementException();
hasRemoved = false;
return current = uniqueItr.next();
}
@Override
public void remove() {
if(hasRemoved)
throw new IllegalStateException();
for(Iterator<T> iterator = collection.iterator(); iterator.hasNext();) {
if(iterator.next().equals(current))
iterator.remove();
}
hasRemoved = true;
}
};
}
(。对于我们而言,我们可以假定,传递的集合不包含null
)
这两个例外都是在写得很好的应用程序中永远不会发生的,所以在实践中我看不到这个问题。 – biziclop
授予,但作为一个完美主义者,它会*使我感到厌烦,'remove()'改变它的异常。我相信它应该总是抛出一个'UnsupportedOperationException',如果底层的Iterator不支持它 - 我只是不知道如何实现这个... – triangular
那么,在这种情况下,我会说你应该记住你是否已经抛出了一个ISE,并且继续扔掉它以用于剩余的物体的寿命。我知道这不是你想要的,但它是一致的。 – biziclop